www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - C++ namespace mangling: bug or me being stupid?

reply Atila Neves <atila.neves gmail.com> writes:
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);
}
Mar 28
parent reply kinke <noone nowhere.com> writes:
That's a mangling compression scheme (possibly tunable via gcc 
options), from 
https://github.com/gchatelet/gcc_cpp_mangling_documentation:
 To save space a compression scheme is used where symbols that 
 appears multiple times are then substituted by an item from the 
 sequence : S_, S0_, S1_, S2_, etc ...
As to nested C++ namespaces - `extern(C++, ns1) { ... extern(C++, ns2) { ... } }` should work in a single D file.
Mar 28
next sibling parent Atila Neves <atila.neves gmail.com> writes:
On Tuesday, 28 March 2017 at 16:30:19 UTC, kinke wrote:
 That's a mangling compression scheme (possibly tunable via gcc 
 options), from 
 https://github.com/gchatelet/gcc_cpp_mangling_documentation:
 To save space a compression scheme is used where symbols that 
 appears multiple times are then substituted by an item from 
 the sequence : S_, S0_, S1_, S2_, etc ...
As to nested C++ namespaces - `extern(C++, ns1) { ... extern(C++, ns2) { ... } }` should work in a single D file.
Good to know, thanks! Atila
Mar 28
prev sibling parent Atila Neves <atila.neves gmail.com> writes:
On Tuesday, 28 March 2017 at 16:30:19 UTC, kinke wrote:
 That's a mangling compression scheme (possibly tunable via gcc 
 options), from 
 https://github.com/gchatelet/gcc_cpp_mangling_documentation:
 To save space a compression scheme is used where symbols that 
 appears multiple times are then substituted by an item from 
 the sequence : S_, S0_, S1_, S2_, etc ...
As to nested C++ namespaces - `extern(C++, ns1) { ... extern(C++, ns2) { ... } }` should work in a single D file.
Not only good to know, this... fixed it. So I was being stupid. Thanks! Atila
Mar 29