D and C++ undefined reference when namespace

Markus contact at markus-lanner.com
Thu Mar 8 15:27:31 UTC 2018


Hi

I got the following c++ code [lib.cpp]:
namespace ns_a
{
   class class_a {
   };
   void some_function(class_a*) {;}
}

and the following d code [main.d]:
extern (C++, namespace_a) {
     class class_a {}
     void some_function(class_a);
}
void main() {
     namespace_a.class_a instance_a;
     namespace_a.some_function(instance_a);
}

I'm compiling lib.cpp to a shared library by:
g++ -shared lib.cpp -o libissue.so
and I'm building and linking it with main.d by
dmd main.d -L-lissue -L-L.

Then I get the error:
main.o: In function `_Dmain':
main.d:(.text._Dmain[_Dmain]+0xa): undefined reference to 
`namespace_a::some_function(namespace_a::class_a*)'
collect2: error: ld returned 1 exit status
Error: linker exited with status 1

When I move the c++ class_a to another namespace (eg. to 
namespace_b), I'm able to compile and link!
readelf -Ws libissue.so | grep some_function
gives me
_ZN4ns_a13some_functionEPNS_7class_aE
so I don't see any issue. Any idea what I'm missing? I guess some 
linker flag when compiling libissue.so

Cheers,
Markus


More information about the Digitalmars-d mailing list