lists.zerezo.com



[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

***BOGO*** Re: [Mingw-users] Why does g++ not link with libstdc++?



fiveight wrote on 14 May 2008 08:01:

> Thank you very much, DaveK.
> 
> I decide to add "-lstdc++" manually with g++.exe.
> 
> I think g++.exe should automatic link with stdc++ library whether there
> are object files in command line. Is this a bug? 

  Yes, you have rediscovered it, and it's fixed in newer versions:
http://gcc.gnu.org/ml/gcc-patches/2004-08/msg00344.html

  In fact, I've just discovered another solution for you.  I tried a few
experiments:

~ $ g++ -o proj.exe
g++: no input files

~ $ g++ -o proj.exe -Wl,-foo
/usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../i686-pc-cygwin/bin/ld: -f may
not be used without -shared
collect2: ld returned 1 exit status

  So, that shows that the presence of -Wl is indeed the only thing causing the
linker to even be invoked.  Now, add -v to see the command-line being used:

~ $ g++ -o proj.exe -Wl,-foobar -v
Reading specs from /usr/lib/gcc/i686-pc-cygwin/3.4.4/specs
Configured with: /managed/gcc-spin-4/gcc-3.4.4-4/configure --verbose
--prefix=/usr --exec-prefix=/usr --sysconfdir=/etc --libdir=/usr/lib
--libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info
--enable-languages=c,ada,c++,d,f77,pascal,java,objc --enable-nls
--without-included-gettext --enable-version-specific-runtime-libs --without-x
--enable-libgcj --disable-java-awt --with-system-zlib --enable-interpreter
--disable-libgcj-debug --enable-threads=posix --enable-java-gc=boehm
--disable-win32-registry --enable-sjlj-exceptions
--enable-hash-synchronization --enable-libstdcxx-debug
Thread model: posix
gcc version 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)
 /usr/lib/gcc/i686-pc-cygwin/3.4.4/collect2.exe -Bdynamic
--dll-search-prefix=cyg -o proj.exe
/usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../crt0.o
-L/usr/lib/gcc/i686-pc-cygwin/3.4.4 -L/usr/lib/gcc/i686-pc-cygwin/3.4.4
-L/usr/lib/gcc/i686-pc-cygwin/3.4.4/../../.. -foobar -lgcc -lcygwin -luser32
-lkernel32 -ladvapi32 -lshell32 -lgcc
/usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../i686-pc-cygwin/bin/ld: -f may
not be used without -shared
collect2: ld returned 1 exit status


  Notice how it /did/ add the standard C libraries (I'm using a cygwin
compiler here, but the principle is the same for mingw, only the list of -l
options would be slightly different).  So, that made me thing that maybe
g++.exe is defaulting to assume a plain old C compilation, because there are
no *.cpp or *.cc source files it can see in the command line to tell it
otherwise.  And in *that* case, maybe we can use the -x option that specifies
the default input langauage:

~ $ g++ -o proj.exe -Wl,-foobar -v -x c++
Reading specs from /usr/lib/gcc/i686-pc-cygwin/3.4.4/specs
Configured with: /managed/gcc-spin-4/gcc-3.4.4-4/configure --verbose
--prefix=/usr --exec-prefix=/usr --sysconfdir=/etc --libdir=/usr/lib
--libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info
--enable-languages=c,ada,c++,d,f77,pascal,java,objc --enable-nls
--without-included-gettext --enable-version-specific-runtime-libs --without-x
--enable-libgcj --disable-java-awt --with-system-zlib --enable-interpreter
--disable-libgcj-debug --enable-threads=posix --enable-java-gc=boehm
--disable-win32-registry --enable-sjlj-exceptions
--enable-hash-synchronization --enable-libstdcxx-debug
Thread model: posix
gcc version 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)
 /usr/lib/gcc/i686-pc-cygwin/3.4.4/collect2.exe -Bdynamic
--dll-search-prefix=cyg -o proj.exe
/usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../crt0.o
-L/usr/lib/gcc/i686-pc-cygwin/3.4.4 -L/usr/lib/gcc/i686-pc-cygwin/3.4.4
-L/usr/lib/gcc/i686-pc-cygwin/3.4.4/../../.. -foobar -lstdc++ -lgcc -lcygwin
-luser32 -lkernel32 -ladvapi32 -lshell32 -lgcc
/usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../i686-pc-cygwin/bin/ld: -f may
not be used without -shared
collect2: ld returned 1 exit status


  And look!  We got our lstdc++!

  So, you could add "-x c++" to your command line.  That's a bit better than
adding -lstdc++ manually because it will always "do the right thing", in the
face of perhaps different multilib or static-vs-shared versions of the
library, and it will also take care of any other options apart from -lstdc++
that might be required when linking c++ executables.

  I've also tried this with a more recent (4.3.0) gcc build I had lying
around, and with that version, using g++ instead of gcc /does/ make it add
-lstdc++.



    cheers,
      DaveK
-- 
Can't think of a witty .sigline today....


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft 
Defy all challenges. Microsoft(R) Visual Studio 2008. 
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
MinGW-users mailing list
MinGW-users@xxxxxxxxxxxxxxxxxxxxx

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users