www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 2480] New: extern(C++) does not work with linux

reply d-bugmail puremagic.com writes:

           Summary: extern(C++) does not work with linux
           Product: D
           Version: 2.014
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Keywords: link-failure
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: jason.james.house gmail.com

foo.cpp file:
struct board{ void clear(){} };

bar.d file:
extern(C++){ struct board{ void clear(); } }
void main(){
  board b;

build steps:
g++ -c foo.cpp -o foo.o
dmd -c bar.d -ofbar.o
gcc foo.o bar.o -L/usr/local/lib -L/usr/lib/gcc/i486-linux-gcc/4.3 -lstdc++ -l
phobos2 -lpthread

This was tried with Ubuntu 8.10 and dmd 2.014

Nov 30 2008
parent d-bugmail puremagic.com writes:

torhu yahoo.com changed:

           What    |Removed                     |Added
                 CC|                            |torhu yahoo.com
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID

------- Comment #1 from torhu yahoo.com  2008-11-30 14:54 -------
That's not supposed to work, see
http://www.digitalmars.com/d/2.0/cpp_interface.html.  Scroll down to "Calling
C++ Virtual Functions From D" on that page.  C++ functions have to be either
global or virtual, and you need to create an interface on the D side.  C++
struct virtual methods won't work, since the name mangling is different than
for classes.  And you need to instantiate and delete C++ objects in C++ code,
not D code.

This works, at least on Windows:

foo.cpp file:
class Board{ public: virtual void clear(){} };

Board* createBoard() { return new Board; }
void freeBoard(Board* b) { delete b; }

bar.d file:
extern(C++) interface Board
    void clear();

extern (C++) Board createBoard();
extern (C++) void freeBoard(Board b);

void main()
  Board b = createBoard();

Nov 30 2008