D and C++ undefined reference when namespace

Markus contact at markus-lanner.com
Thu Mar 8 16:23:46 UTC 2018


On Thursday, 8 March 2018 at 15:27:31 UTC, Markus wrote:
> 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

https://forum.dlang.org/thread/mailman.2458.1448772039.22025.digitalmars-d@puremagic.com
seems to describe my issue.
To quote Walter Wright
> D does not support C++ semantics. You cannot split namespaces 
> into multiple files in D, nor can you add symbols to an 
> existing namespace. For namespace NS, all the declarations in 
> NS have to be in one file and between the { }, just like any 
> other scope in D.
in my opinion, that's really annoying for C++ wrapper devs.

I failed to describe my issue in the first post.
there has to be another.d
the function and class have to be in seperate d files.

Sorry for the spam


More information about the Digitalmars-d mailing list