C++ namespace mangling: bug or me being stupid?

Atila Neves via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Mar 28 08:54:08 PDT 2017


I'm trying to wrap a C++ library and have reduced my problem case 
to the code below. I get a linker error due to different name 
mangling (this is on Linux):

main.d:(.text._Dmain+0x13): undefined reference to 
`_ZN3ns13ns212createStructERN3ns17OptionsE'

The C++ object file has instead a symbol called 
_ZN3ns13ns212createStructERNS_7OptionsE. To make matters worse 
and me more confused, according to https://demangler.com, they 
both demangle to the same thing: 
`ns1::ns2::createStruct(ns1::Options&)`.

If you're wondering why there's two D files it's because trying 
to do:

extern(C++, ns1) {... }
extern(C++, ns1.ns2) {...}

doesn't compile since there would be two D versions of ns1, which 
is... mildly annoying.

I even took `const` off of the parameter in case that was the 
problem but clearly not.

Atila


impl.cpp:


namespace ns1 {

struct Options {

};

struct Struct {

};

namespace ns2 {
ns1::Struct* createStruct(Options&) {
     return nullptr;
}

}

}


header.d:

extern(C++, ns1) {
     struct Options {

     }

     struct Struct {
     }
}


main.d:

extern(C++, ns1.ns2) {
     import header;
     Struct* createStruct(ref Options);
}


void main() {
     import header;
     Options options;
     auto s = createStruct(options);
}




More information about the Digitalmars-d-learn mailing list