www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Linking C++ standard library works with GDC... but not DMD. (Linux)

reply "TheGag96" <thegag96 gmail.com> writes:
Hi, I've got this project that requires me to link into a C++
backend. It works just fine when using GDC:

gdc *.d [client libraries]

However, this command using DMD does not work:

dmd -L-lstdc++ *.d [client libraries]

I still get errors involving the standard library not being added
like:

/usr/include/c++/4.8/iostream:74: undefine reference to
`std::ios_base::Init::Init()'
(etc.)

What do I need to do to make this work? Thanks.
Apr 16 2015
next sibling parent "FreeSlave" <freeslave93 gmail.com> writes:
On Thursday, 16 April 2015 at 08:51:15 UTC, TheGag96 wrote:
 Hi, I've got this project that requires me to link into a C++
 backend. It works just fine when using GDC:

 gdc *.d [client libraries]

 However, this command using DMD does not work:

 dmd -L-lstdc++ *.d [client libraries]

 I still get errors involving the standard library not being 
 added
 like:

 /usr/include/c++/4.8/iostream:74: undefine reference to
 `std::ios_base::Init::Init()'
 (etc.)

 What do I need to do to make this work? Thanks.
Can't get to linux machine now, but you can try place -L-lstdc++ after source files.
Apr 16 2015
prev sibling next sibling parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
On Thursday, 16 April 2015 at 08:51:15 UTC, TheGag96 wrote:
 Hi, I've got this project that requires me to link into a C++
 backend. It works just fine when using GDC:

 gdc *.d [client libraries]

 However, this command using DMD does not work:

 dmd -L-lstdc++ *.d [client libraries]

 I still get errors involving the standard library not being 
 added
 like:

 /usr/include/c++/4.8/iostream:74: undefine reference to
 `std::ios_base::Init::Init()'
 (etc.)

 What do I need to do to make this work? Thanks.
I don't know why that's happening, but you can explicitly link using gcc (or ld/gold directly) if you need to. E.g. dmd -c myFile.d gcc myFile.o -lphobos2 or whatever is necessary on your project/system.
Apr 16 2015
parent "TheGag96" <thegag96 gmail.com> writes:
On Thursday, 16 April 2015 at 09:46:40 UTC, John Colvin wrote:
 On Thursday, 16 April 2015 at 08:51:15 UTC, TheGag96 wrote:
 Hi, I've got this project that requires me to link into a C++
 backend. It works just fine when using GDC:

 gdc *.d [client libraries]

 However, this command using DMD does not work:

 dmd -L-lstdc++ *.d [client libraries]

 I still get errors involving the standard library not being 
 added
 like:

 /usr/include/c++/4.8/iostream:74: undefine reference to
 `std::ios_base::Init::Init()'
 (etc.)

 What do I need to do to make this work? Thanks.
I don't know why that's happening, but you can explicitly link using gcc (or ld/gold directly) if you need to. E.g. dmd -c myFile.d gcc myFile.o -lphobos2 or whatever is necessary on your project/system.
I had wanted to try this too, but I wasn't sure if the linker knew where phobos2 was on my system. I just tried this now for funsies: dmd -c *.d g++ main.o [client files] -lphobos2 With or without that -lphobos2 flag, I get errors like these: main.d(.text._Dmain+0x18e): undefined reference to `D3std5stdio16__T7writeln(TAyaZ7writeFAyaZv'
Apr 16 2015
prev sibling next sibling parent reply "Dicebot" <public dicebot.lv> writes:
On Thursday, 16 April 2015 at 08:51:15 UTC, TheGag96 wrote:
 Hi, I've got this project that requires me to link into a C++
 backend. It works just fine when using GDC:

 gdc *.d [client libraries]

 However, this command using DMD does not work:

 dmd -L-lstdc++ *.d [client libraries]

 I still get errors involving the standard library not being 
 added
 like:

 /usr/include/c++/4.8/iostream:74: undefine reference to
 `std::ios_base::Init::Init()'
 (etc.)

 What do I need to do to make this work? Thanks.
Simple issue but unpleasant fix. You always must use C++ library that matches base C++ compiler. For GDC it is GCC (which is used by default). For DMD it is DMC (Digital Mars C compiler). For LDC it is whatever Clang standard library is called. All those are incompatible because rely on different compiler built-ins.
Apr 16 2015
parent reply Jacob Carlborg <doob me.com> writes:
On 2015-04-16 11:56, Dicebot wrote:

 Simple issue but unpleasant fix. You always must use C++ library that
 matches base C++ compiler. For GDC it is GCC (which is used by default).
 For DMD it is DMC (Digital Mars C compiler). For LDC it is whatever
 Clang standard library is called. All those are incompatible because
 rely on different compiler built-ins.
The title says (Linux), where DMD uses GCC and not DMC. -- /Jacob Carlborg
Apr 16 2015
parent reply "Dicebot" <public dicebot.lv> writes:
On Thursday, 16 April 2015 at 12:39:07 UTC, Jacob Carlborg wrote:
 On 2015-04-16 11:56, Dicebot wrote:

 Simple issue but unpleasant fix. You always must use C++ 
 library that
 matches base C++ compiler. For GDC it is GCC (which is used by 
 default).
 For DMD it is DMC (Digital Mars C compiler). For LDC it is 
 whatever
 Clang standard library is called. All those are incompatible 
 because
 rely on different compiler built-ins.
The title says (Linux), where DMD uses GCC and not DMC.
Does DMD also use GCC conventions on Linux when compiling extern(C++) code? I didn't know that.
Apr 17 2015
parent Jacob Carlborg <doob me.com> writes:
On 2015-04-17 10:16, Dicebot wrote:

 Does DMD also use GCC conventions on Linux when compiling extern(C++)
 code? I didn't know that.
Not exactly sure that you mean by "conventions" but "extern(C++)" and "extern(C)" follows the ABI of the system compiler. On Linux that is GCC, on OS X it's Clang/GCC, on Windows it's either DMC or VS depending on what object format is used, I guess. DMC is not even available for any other platform than Windows. -- /Jacob Carlborg
Apr 17 2015
prev sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 4/16/15 4:51 AM, TheGag96 wrote:
 Hi, I've got this project that requires me to link into a C++
 backend. It works just fine when using GDC:

 gdc *.d [client libraries]

 However, this command using DMD does not work:

 dmd -L-lstdc++ *.d [client libraries]

 I still get errors involving the standard library not being added
 like:

 /usr/include/c++/4.8/iostream:74: undefine reference to
 `std::ios_base::Init::Init()'
 (etc.)
Try dmd -v to tell you exactly what command it is running for link. Then play around with the link line to see if you can figure out a way to link it correctly. We can work on fixing the situation from there. Maybe there's a way to link using dmd, maybe we have to fix dmd so it allows the correct link call. -Steve
Apr 16 2015
parent "TheGag96" <thegag96 gmail.com> writes:
On Thursday, 16 April 2015 at 12:57:12 UTC, Steven Schveighoffer 
wrote:
 /usr/include/c++/4.8/iostream:74: undefine reference to
 `std::ios_base::Init::Init()'
 (etc.)
Try dmd -v to tell you exactly what command it is running for link. Then play around with the link line to see if you can figure out a way to link it correctly. We can work on fixing the situation from there. Maybe there's a way to link using dmd, maybe we have to fix dmd so it allows the correct link call. -Steve
Got it!! This is what helped me the most. I compiled a regular C++ program with the -v flag, found where my libstdc++.a was, added it to the command and bam. I really should have thought of this one earlier... Thanks for the quick support, everyone!
Apr 16 2015