www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Nested C++ namespace library linking

reply "Guillaume Chatelet" <chatelet.guillaume gmail.com> writes:
Consider the following foo.cpp

namespace A {
namespace B {
   struct Type {};
   int foo(Type unused){ return 42; }
}
}

Compile it : g++ foo.cpp -c -o foo.o

Then the following main.d

extern(C++, A.B) {
   struct Type {}
   int foo(Type unused);
}

void main() {
   foo(Type());
}

Compile it : dmd main.d foo.o
It fails with : undefined reference to « A::B::foo(A::Type) »

It looks like the Type is not resolved in the right namespace. 
A::Type instead of A::B::Type. Did I miss something or is this a 
bug ?

I also tried fully qualifying foo and Type but I end up with the 
exact same error :
   A.B.foo(A.B.Type());
Jan 20 2015
next sibling parent reply "Paul O'Neil" <redballoon36 gmail.com> writes:
On Tuesday, 20 January 2015 at 21:10:59 UTC, Guillaume Chatelet 
wrote:
 Consider the following foo.cpp

 namespace A {
 namespace B {
   struct Type {};
   int foo(Type unused){ return 42; }
 }
 }

 Compile it : g++ foo.cpp -c -o foo.o

 Then the following main.d

 extern(C++, A.B) {
   struct Type {}
   int foo(Type unused);
 }

 void main() {
   foo(Type());
 }

 Compile it : dmd main.d foo.o
 It fails with : undefined reference to « A::B::foo(A::Type) »

 It looks like the Type is not resolved in the right namespace. 
 A::Type instead of A::B::Type. Did I miss something or is this 
 a bug ?

 I also tried fully qualifying foo and Type but I end up with 
 the exact same error :
   A.B.foo(A.B.Type());
Looks like a bug to me. --- Paul O'Neil Github / IRC: todayman
Jan 20 2015
parent reply "Guillaume Chatelet" <chatelet.guillaume gmail.com> writes:
That's what I thought.
I reported this bug a while ago but it didn't get a lot of 
attention.
https://issues.dlang.org/show_bug.cgi?id=13337
Jan 20 2015
parent "Kagamin" <spam here.lot> writes:
You can ask to add a keyword to bugzilla for C++ issues, this can 
help to improve their visibility and searchability.
Jan 21 2015
prev sibling parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
On Tuesday, 20 January 2015 at 21:10:59 UTC, Guillaume Chatelet 
wrote:
 Consider the following foo.cpp

 namespace A {
 namespace B {
   struct Type {};
   int foo(Type unused){ return 42; }
 }
 }

 Compile it : g++ foo.cpp -c -o foo.o

 Then the following main.d

 extern(C++, A.B) {
   struct Type {}
   int foo(Type unused);
 }

 void main() {
   foo(Type());
 }

 Compile it : dmd main.d foo.o
 It fails with : undefined reference to « A::B::foo(A::Type) »

 It looks like the Type is not resolved in the right namespace. 
 A::Type instead of A::B::Type. Did I miss something or is this 
 a bug ?

 I also tried fully qualifying foo and Type but I end up with 
 the exact same error :
   A.B.foo(A.B.Type());
Looks like a bug to me, for sure. In the mean-time you may be able to use some pragma(mangle, ...) hacks to force the compiler to emit the right symbols.
Jan 21 2015
parent "Guillaume Chatelet" <chatelet.guillaume gmail.com> writes:
On Wednesday, 21 January 2015 at 14:59:15 UTC, John Colvin wrote:
 Looks like a bug to me, for sure.

 In the mean-time you may be able to use some pragma(mangle, 
 ...) hacks to force the compiler to emit the right symbols.
Thx John, extern(C++, A.B) { struct Type {} pragma(mangle,"_ZN1A1B3fooENS0_4TypeE") int foo(Type unused); } is indeed linking correctly :)
Jan 21 2015