D and C++ undefined reference when namespace

Steven Schveighoffer schveiguy at yahoo.com
Thu Mar 8 17:04:02 UTC 2018


On 3/8/18 11:35 AM, Markus wrote:

> 
> error:
> main.o: In function `_Dmain':
> main.d:(.text._Dmain[_Dmain]+0xa): undefined reference to 
> `ns_a::some_function(ns_a::class_a*)'
> collect2: error: ld returned 1 exit status
> Error: linker exited with status 1
> 
> symbols:
> nm --demangle libissue.so | some_function
> 000000000000059a T ns_a::some_function(ns_a::class_a*)
> 
> It doesn't seem like an error, but it is.
> I still don't get it, whey I'm not allowed to split the namespace 
> declarations.
> 

This is a linker error. Your D code is compiling just fine (in other 
words, the aforementioned issue is not happening to you), it's just not 
getting the definition from the dynamic library.

When I do this locally on my mac, I get a similar error. When I nm the 
main.o file vs. the lib.o file, I see different mangled names.

It appears that the D mangled name is not doing back references. This is 
probably why the change to another namespace for the function works 
(there isn't a back reference)

In order to demonstrate this better, I did namespace thenamespace 
instead of ns_a.

The symbol I see in the D file:
U __ZN12thenamespace13some_functionEPN12thenamespace7class_aE

And in the C++ file:
T __ZN12thenamespace13some_functionEPNS_7class_aE

Note the difference is instead of 12thenamespace, it's S_, which 
probably is a back reference. Googled...

Yep, I'm right: 
https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangle.seq-id

I'd recommend filing a bug.

-Steve


More information about the Digitalmars-d mailing list