Bug 2480 – extern(C++) does not work with linux

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Linux
Creation time
2008-11-30T11:10:00Z
Last change time
2015-06-09T01:21:36Z
Keywords
link-failure
Assigned to
bugzilla
Creator
jason.james.house

Comments

Comment #0 by jason.james.house — 2008-11-30T11:10:52Z
foo.cpp file: struct board{ void clear(){} }; bar.d file: extern(C++){ struct board{ void clear(); } } void main(){ board b; b.clear(); } 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
Comment #1 by torhu — 2008-11-30T14:54:20Z
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(); b.clear(); freeBoard(b); }