www.digitalmars.com         C & C++   DMDScript  

D - Legacy code.

reply John Fletcher <J.P.Fletcher aston.ac.uk> writes:
Thinking about D, I keep thinking, I would like to interface it to
things I have already.  These are mostly in C++.

I have met this problem before, as I already interface FORTRAN and C++.

Please do not make it impossible to carry over things which work.

John
Aug 21 2001
next sibling parent "Rajiv Bhagwat" <dataflow vsnl.com> writes:
I wanted to start a separate thread 'compatibility rules!' - but this is
close enough.

Taking several steps back, any language called 'D' should stright-away
compile 'c' AND 'c++' programs - after all, one of the reasons for the
acceptance for c++ was that it 'included' c. The object-oriented pundits
have argued enough about c++ not being enough so, but it got accepted.

In the same spirit, what is the possibility of 'D' accepting 'c' and 'c++'
unchanged and adding (to the mess <g>) features to evolve a better language.
In any case, ability to interface to 'c' has brought this halfway.

One way, for example, to introduce a different switch statement would be to
use a different keyword, say 'Switch', this way one can use 'switch' and
'Switch'. The usage of:
#include "stdlib.h"
import stdio;
is clear enough.

Ok, in case such an extension is likely to carry too many bad things
forward, how about keeking c, cpp and d code in different files and the
compiler applying the appropriate rules to appropriate files within a
project?

All this is based on the simple fact that more than anyone else in the
world, Walter is in a postion to do this, owing to already having a ready,
actively maintained, evolving C++ compiler.

This way, the existing c++ code may be fully utilised in 'D' projects and
the C++ compiler will also come closer and closer to the ANSI standard
(which, otherwise, I am afraid will not happen due the 'D' hoopla!)
-- Rajiv Bhagwat


John Fletcher <J.P.Fletcher aston.ac.uk> wrote in message
news:3B822385.B2298DD8 aston.ac.uk...
 Thinking about D, I keep thinking, I would like to interface it to
 things I have already.  These are mostly in C++.

 I have met this problem before, as I already interface FORTRAN and C++.

 Please do not make it impossible to carry over things which work.

 John
Aug 21 2001
prev sibling next sibling parent "Johan Bryssling" <johan.bryssling micronet.se> writes:
Hi!

I think allowing linking to a C/C++ library (DLL, SO files) could be a nice
approach.... without involing recompling of sourcecode....

regards

/Johan Bryssling, Software engineer , Micronet

"John Fletcher" <J.P.Fletcher aston.ac.uk> wrote in message
news:3B822385.B2298DD8 aston.ac.uk...
 Thinking about D, I keep thinking, I would like to interface it to
 things I have already.  These are mostly in C++.

 I have met this problem before, as I already interface FORTRAN and C++.

 Please do not make it impossible to carry over things which work.

 John
Aug 21 2001
prev sibling parent reply "Walter" <walter digitalmars.com> writes:
Interfacing to C++ objects is just way too complicated. -Walter

"John Fletcher" <J.P.Fletcher aston.ac.uk> wrote in message
news:3B822385.B2298DD8 aston.ac.uk...
 Thinking about D, I keep thinking, I would like to interface it to
 things I have already.  These are mostly in C++.

 I have met this problem before, as I already interface FORTRAN and C++.

 Please do not make it impossible to carry over things which work.

 John
Aug 21 2001
parent reply Jan Knepper <jan smartsoft.cc> writes:
There is probably a solution most of the times by interfacing to C++ via a C
wrapper?

I mean you should be able to

C++:
class CX
{
    public    :
        CX ()
        void        Print    ();
};


D:
class  DX
{
    private   :
        void    *cx;            // <g>

    public    :
        this () { cx = CreateCX (); }
        ~this () { DeleteCX ( cx ); }

        void        Print () { PrintCX ( cx ); }
};


C:
void  *CreateCX ()
{
    return ( new  CX () );
}

void  DeleteCX ( void  *ptr )
{
    delete ( ( CX * ) ptr );
}

void  PrintCX ( void  *ptr )
{
    ( ( CX * ) ptr ) -> Print ();
}

OK, it's a little work, but I think it would work.

Jan



Walter wrote:

 Interfacing to C++ objects is just way too complicated. -Walter

 "John Fletcher" <J.P.Fletcher aston.ac.uk> wrote in message
 news:3B822385.B2298DD8 aston.ac.uk...
 Thinking about D, I keep thinking, I would like to interface it to
 things I have already.  These are mostly in C++.

 I have met this problem before, as I already interface FORTRAN and C++.

 Please do not make it impossible to carry over things which work.

 John
-- Jan Knepper Smartsoft, LLC 88 Petersburg Road Petersburg, NJ 08270 U.S.A. http://www.smartsoft.cc/ Phone : 609-628-4260 FAX : 609-628-1267 In God we Trust -- all others must submit an X.509 certificate. -- Charles Forsythe <forsythe alum.mit.edu>
Aug 21 2001
parent reply "Walter" <walter digitalmars.com> writes:
Since there is an interface to C, a C wrapper around C++ *must* work, since
D cannot tell the difference!

Jan Knepper wrote in message <3B829BD2.EF94023D smartsoft.cc>...
There is probably a solution most of the times by interfacing to C++ via a
C
wrapper?

I mean you should be able to

C++:
class CX
{
    public    :
        CX ()
        void        Print    ();
};


D:
class  DX
{
    private   :
        void    *cx;            // <g>

    public    :
        this () { cx = CreateCX (); }
        ~this () { DeleteCX ( cx ); }

        void        Print () { PrintCX ( cx ); }
};


C:
void  *CreateCX ()
{
    return ( new  CX () );
}

void  DeleteCX ( void  *ptr )
{
    delete ( ( CX * ) ptr );
}

void  PrintCX ( void  *ptr )
{
    ( ( CX * ) ptr ) -> Print ();
}

OK, it's a little work, but I think it would work.

Jan



Walter wrote:

 Interfacing to C++ objects is just way too complicated. -Walter

 "John Fletcher" <J.P.Fletcher aston.ac.uk> wrote in message
 news:3B822385.B2298DD8 aston.ac.uk...
 Thinking about D, I keep thinking, I would like to interface it to
 things I have already.  These are mostly in C++.

 I have met this problem before, as I already interface FORTRAN and C++.

 Please do not make it impossible to carry over things which work.

 John
-- Jan Knepper Smartsoft, LLC 88 Petersburg Road Petersburg, NJ 08270 U.S.A. http://www.smartsoft.cc/ Phone : 609-628-4260 FAX : 609-628-1267 In God we Trust -- all others must submit an X.509 certificate. -- Charles Forsythe <forsythe alum.mit.edu>
Aug 22 2001
parent reply Damian Dixon <damiandixon netscape.net> writes:
One of the problems with wrapping C++ with a C interface, is how do you 
ensure initialization of the C++ (and C) libraries. Normally the only 
way is to use the C++ linker/compiler to perform the final compile/link 
to ensure that the static initializes and constructors are called before 
main is called.

Unless of course you have your own program initialization code. However
this would require knowledge of both the C++, C and D compiler's methods
for initializing a program at start-up (before main is called).

I'm very sure Walter is aware of this... :> and could quite easily build
the support into the D compiler, so that it supports Digital Mars C/C++ 
compiled code.

I would however like to refer to the C++ standard, sub clause 7.5/3 and 
7.5/9, which only mentions linkage of C from C++, the C standard does 
not mention linkage to C++.

However lucky for us almost all compilers I have used allows the calling 
from C to C++ when the method in C++'s linkage is specified as 'extern 
"C"', as long as the C and C++ code was compiled using the same vendors 
C and C++ compiler and the source module containing the main function 
was compiled and linked with the C++ compiler.

Regards,
Damian

Walter wrote:

 Since there is an interface to C, a C wrapper around C++ *must* work, since
 D cannot tell the difference!
 
 Jan Knepper wrote in message <3B829BD2.EF94023D smartsoft.cc>...
 
There is probably a solution most of the times by interfacing to C++ via a
C
wrapper?

I mean you should be able to

C++:
class CX
{
   public    :
       CX ()
       void        Print    ();
};


D:
class  DX
{
   private   :
       void    *cx;            // <g>

   public    :
       this () { cx = CreateCX (); }
       ~this () { DeleteCX ( cx ); }

       void        Print () { PrintCX ( cx ); }
};


C:
void  *CreateCX ()
{
   return ( new  CX () );
}

void  DeleteCX ( void  *ptr )
{
   delete ( ( CX * ) ptr );
}

void  PrintCX ( void  *ptr )
{
   ( ( CX * ) ptr ) -> Print ();
}

OK, it's a little work, but I think it would work.

Jan



Walter wrote:


Interfacing to C++ objects is just way too complicated. -Walter

"John Fletcher" <J.P.Fletcher aston.ac.uk> wrote in message
news:3B822385.B2298DD8 aston.ac.uk...

Thinking about D, I keep thinking, I would like to interface it to
things I have already.  These are mostly in C++.

I have met this problem before, as I already interface FORTRAN and C++.

Please do not make it impossible to carry over things which work.

John
-- Jan Knepper Smartsoft, LLC 88 Petersburg Road Petersburg, NJ 08270 U.S.A. http://www.smartsoft.cc/ Phone : 609-628-4260 FAX : 609-628-1267 In God we Trust -- all others must submit an X.509 certificate. -- Charles Forsythe <forsythe alum.mit.edu>
Aug 22 2001
parent John Fletcher <J.P.Fletcher aston.ac.uk> writes:
Damian Dixon wrote:

 However lucky for us almost all compilers I have used allows the calling
 from C to C++ when the method in C++'s linkage is specified as 'extern
 "C"', as long as the C and C++ code was compiled using the same vendors
 C and C++ compiler and the source module containing the main function
 was compiled and linked with the C++ compiler.

 Regards,
 Damian
I have used this method to provide C linkage calls to C++, actually calls from FORTRAN. (SALFORD compilers for both FORTRAN and C++) John
Aug 22 2001