What should I fix in the Makefile?
jalal#klein:~/computer_vision/mmglm_neuroimage_private-master/lib/c++/src$ make
g++ -g -Wall -Wextra -Wunreachable-code -static mmglm_spd_multithread.cpp -o ../bin/mmglm_spd_par -fopenmp -O3 -llapack -lblas -lpthread -lgfortran -lboost_system -lboost_filesystem -I ../lib/armadillo-3.930.2/include/
mmglm_spd_multithread.cpp: In function ‘int main(int, char**)’:
mmglm_spd_multithread.cpp:117:10: warning: unused variable ‘length’ [-Wunused-variable]
size_t length=0;//initializing length as a counter
^
/usr/lib/gcc/x86_64-linux-gnu/5/libgomp.a(target.o): In function `gomp_target_init':
(.text+0xba): warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/x86_64-linux-gnu/5/libgfortran.a(write.o): In function `write_float':
(.text.write_float+0xb2): undefined reference to `signbitq'
/usr/lib/gcc/x86_64-linux-gnu/5/libgfortran.a(write.o): In function `write_float':
(.text.write_float+0xbf): undefined reference to `finiteq'
/usr/lib/gcc/x86_64-linux-gnu/5/libgfortran.a(write.o): In function `write_float':
(.text.write_float+0x133): undefined reference to `quadmath_snprintf'
/usr/lib/gcc/x86_64-linux-gnu/5/libgfortran.a(write.o): In function `write_float':
(.text.write_float+0x254): undefined reference to `finiteq'
/usr/lib/gcc/x86_64-linux-gnu/5/libgfortran.a(write.o): In function `write_float':
(.text.write_float+0xaff): undefined reference to `isnanq'
/usr/lib/gcc/x86_64-linux-gnu/5/libgfortran.a(write.o): In function `write_float':
(.text.write_float+0xb83): undefined reference to `quadmath_snprintf'
/usr/lib/gcc/x86_64-linux-gnu/5/libgfortran.a(write.o): In function `write_float':
(.text.write_float+0xf7d): undefined reference to `quadmath_snprintf'
/usr/lib/gcc/x86_64-linux-gnu/5/libgfortran.a(write.o): In function `write_float':
(.text.write_float+0x10d8): undefined reference to `quadmath_snprintf'
collect2: error: ld returned 1 exit status
Makefile:13: recipe for target 'multithread' failed
make: *** [multithread] Error 1
I have:
$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 5.4.0-6ubuntu1~16.04.2' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-5 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.2)
and
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 16.04.1 LTS
Release: 16.04
Codename: xenial
Here's the related lines from Makefile:
CC= g++
CFLAGS= -Wall -Wextra -Wunreachable-code -static
#-static
#INC_PATH= -I ../lib/armadillo-7.100.3/include/
INC_PATH= -I ../lib/armadillo-3.930.2/include/
LIB_PATH= -llapack -lblas -lpthread -lgfortran -lboost_system -lboost_filesystem
#LIB_PATH= -DARMA_DONT_USE_WRAPPER
#lrefblas
all: multithread mmglm show_arma_mx armabin2asciifull armabinequaltotxt
multithread_fast:
$(CC) -g $(CFLAGS) mmglm_spd_multithread_fast.cpp -o ../bin/mmglm_spd_par_fast -fopenmp -O3 $(LIB_PATH) $(INC_PATH)
multithread:
$(CC) -g $(CFLAGS) mmglm_spd_multithread.cpp -o ../bin/mmglm_spd_par -fopenmp -O3 $(LIB_PATH) $(INC_PATH)
I even tried having -lquadmath flag but got the same error:
multithread:
$(CC) -g $(CFLAGS) mmglm_spd_multithread.cpp -o ../bin/mmglm_spd_par -fopenmp -lquadmath -O3 $(LIB_PATH) $(INC_PATH)
I also have the following packages installed:
2043 sudo apt-get install liblapack-dev
2044 sudo apt-get install liblapacke
2045 sudo apt-get install liblapacke-dev
2046 sudo apt-get install libblas-dev
2048 sudo apt-get install gfortran
2049 sudo apt-get install libatlas-base-dev
2050 sudo apt-get install liblapack3
2052 sudo apt-get install libarmadillo-dev
2056 sudo apt-get install libarmadillo6
2057 sudo apt-get install libarmadillo6-dbgsym
2071 sudo apt-get install libboost-dev
2077 sudo apt-get install libboost-system-dev
2080 sudo apt-get install libboost-filesystem-dev
Solved the problem by adding -lquadmath at the end of this line:
LIB_PATH= -llapack -lblas -lpthread -lgfortran -lboost_system -lboost_filesystem -lquadmath
Related
Iam currently struggeling with linking just a simple library.
I compiled and installed successfully the G729 implementation from here Belledonne G729 like this:
cmake . -DCMAKE_INSTALL_PREFIX=/usr
make
make install
When i check the if the symbols exist they exsist:
nm /usr/lib64/libbcg729.so | less
....
0000000000001fb8 T _init
000000000000de40 T initBcg729CNGChannel
00000000000046dd T initBcg729DecoderChannel
000000000000f5c6 T initBcg729DTXChannel
0000000000005353 T initBcg729EncoderChannel
0000000000010107 T initBcg729VADChannel
....
But when i try to compile this simple main.cpp:
#include <bcg729/decoder.h> // for bcg729DecoderChannelContextStruct
int main()
{
bcg729DecoderChannelContextStruct* decoderChannelContext = initBcg729DecoderChannel();
return 0;
}
I get this as result:
c++ -v main.o -o main -lbcg729
Using built-in specs.
COLLECT_GCC=c++
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
COMPILER_PATH=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/:/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/:/usr/libexec/gcc/x86_64-redhat-linux/:/usr/lib/gcc/x86_64-redhat-linux/4.8.5/:/usr/lib/gcc/x86_64-redhat-linux/
LIBRARY_PATH=/usr/lib/gcc/x86_64-redhat-linux/4.8.5/:/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64/:/lib/../lib64/:/usr/lib/../lib64/:/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-o' 'main' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/collect2 --build-id --no-add-needed --eh-frame-hdr --hash-style=gnu -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o main /usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64/crt1.o /usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64/crti.o /usr/lib/gcc/x86_64-redhat-linux/4.8.5/crtbegin.o -L/usr/lib/gcc/x86_64-redhat-linux/4.8.5 -L/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../.. main.o -lbcg729 -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/lib/gcc/x86_64-redhat-linux/4.8.5/crtend.o /usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64/crtn.o
main.o: In function `main':
/opt/cppplayground/main.cpp:39: undefined reference to `initBcg729DecoderChannel()'
collect2: error: ld returned 1 exit status
I dont get where the issue is here, since if the library wouldnt be found, the linker would complain it does not exist. But in this case the library exists and the function i want to use does also exists.
Woulbe be nice if anyone could help ;)
Thanks in advance!
Found it ....
Since it's a C-Library i forgot to add the 'extern "C" {}' around the includes.
first of all I've searched this one quite a bit and haven't managed to find a question with the same situation.
I'm building a 32 bit plugin as a shared-object on a 64 bit machine and using cpp-redis. cpp-redis built fine and I used a cmake toolchain to force 32 bit, checked the .o files during build and confirmed they are 32 bit.
I compiled and linked my very basic program (consisting of one main source file and an extra lib for talking to my host application since this is a plugin).
Upon running loading the plugin into the host app, I am greeted with:
symbol lookup error: plugins/samp-redis.so: undefined symbol: _ZN9cpp_redis16redis_subscriberC1ERKSt10shared_ptrINS_7network10io_serviceEE
I'm pretty stumped as I've added the cpp-redis dir to the linker path with -L and the library with -l, here's my comp/link lines:
(SDK_DIR is my host app sdk, pretty small and minimal set of .c/h files)
g++ -fpermissive -fPIC -m32 -std=c++11 -c -O3 -w -D LINUX -I$(SDK_DIR) -I$(SDK_DIR)/amx -I/usr/local/include/cpp_redis (source .cpp files...)
g++ -v -Wall -O2 -m32 -fshort-wchar -s -shared -L/usr/local/lib/ -lcpp_redis -o $(OUTFILE) *.o
Which runs fine with no errors, I ran the link stage with -v to check the flags went in properly and it all looks fine.
(added some newlines for readability)
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.9/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure
-v
--with-pkgversion='Debian 4.9.2-10'
--with-bugurl=file:///usr/share/doc/gcc-4.9/README.Bugs
--enable-languages=c,c++,java,go,d,fortran,objc,obj-c++
--prefix=/usr
--program-suffix=-4.9
--enable-shared
--enable-linker-build-id
--libexecdir=/usr/lib
--without-included-gettext
--enable-threads=posix
--with-gxx-include-dir=/usr/include/c++/4.9
--libdir=/usr/lib
--enable-nls
--with-sysroot=/
--enable-clocale=gnu
--enable-libstdcxx-debug
--enable-libstdcxx-time=yes
--enable-gnu-unique-object
--disable-vtable-verify
--enable-plugin
--with-system-zlib
--disable-browser-plugin
--enable-java-awt=gtk
--enable-gtk-cairo
--with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.9-amd64/jre
--enable-java-home
--with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.9-amd64
--with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.9-amd64
--with-arch-directory=amd64
--with-ecj-jar=/usr/share/java/eclipse-ecj.jar
--enable-objc-gc
--enable-multiarch
--with-arch-32=i586
--with-abi=m64
--with-multilib-list=m32,m64,mx32
--enable-multilib
--with-tune=generic
--enable-checking=release
--build=x86_64-linux-gnu
--host=x86_64-linux-gnu
--target=x86_64-linux-gnu
Thread model: posix
gcc version 4.9.2 (Debian 4.9.2-10)
COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.9/
:/usr/lib/gcc/x86_64-linux-gnu/4.9/
:/usr/lib/gcc/x86_64-linux-gnu/
:/usr/lib/gcc/x86_64-linux-gnu/4.9/
:/usr/lib/gcc/x86_64-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.9/32/
:/usr/lib/gcc/x86_64-linux-gnu/4.9/../../../../lib32/
:/lib/../lib32/
:/usr/lib/../lib32/
:/usr/lib/gcc/x86_64-linux-gnu/4.9/
:/usr/lib/gcc/x86_64-linux-gnu/4.9/../../../
:/lib/
:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-Wall' '-O2' '-m32' '-fshort-wchar' '-s' '-shared' '-L/usr/local/lib/' '-o' 'samp-redis.so' '-shared-libgcc' '-mtune=generic' '-march=i586'
/usr/lib/gcc/x86_64-linux-gnu/4.9/collect2
-plugin /usr/lib/gcc/x86_64-linux-gnu/4.9/liblto_plugin.so
-plugin-opt=/usr/lib/gcc/x86_64-linux-gnu/4.9/lto-wrapper
-plugin-opt=-fresolution=/tmp/cc0Lo5po.res
-plugin-opt=-pass-through=-lgcc_s
-plugin-opt=-pass-through=-lc
-plugin-opt=-pass-through=-lgcc_s
--sysroot=/
--build-id
--eh-frame-hdr
-m elf_i386
--hash-style=gnu
-shared
-o samp-redis.so
-s /usr/lib/gcc/x86_64-linux-gnu/4.9/../../../../lib32/crti.o /usr/lib/gcc/x86_64-linux-gnu/4.9/32/crtbeginS.o
-L/usr/local/lib/
-L/usr/lib/gcc/x86_64-linux-gnu/4.9/32
-L/usr/lib/gcc/x86_64-linux-gnu/4.9/../../../../lib32
-L/lib/../lib32
-L/usr/lib/../lib32
-L/usr/lib/gcc/x86_64-linux-gnu/4.9
-L/usr/lib/gcc/x86_64-linux-gnu/4.9/../../..
-lcpp_redis amxplugin2.o amxplugin.o main.o
-lstdc++
-lm
-lgcc_s
-lc
-lgcc_s /usr/lib/gcc/x86_64-linux-gnu/4.9/32/crtendS.o /usr/lib/gcc/x86_64-linux-gnu/4.9/../../../../lib32/crtn.o
Am I misunderstanding the linking stage or statically linking? It has been a while since I did C++... I'm all about Python and Go these days!
EDIT:
Compiling with -W,--no-undefined results in this output:
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.9/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure
-v
--with-pkgversion='Debian 4.9.2-10'
--with-bugurl=file:///usr/share/doc/gcc-4.9/README.Bugs
--enable-languages=c,c++,java,go,d,fortran,objc,obj-c++
--prefix=/usr
--program-suffix=-4.9
--enable-shared
--enable-linker-build-id
--libexecdir=/usr/lib
--without-included-gettext
--enable-threads=posix
--with-gxx-include-dir=/usr/include/c++/4.9
--libdir=/usr/lib
--enable-nls
--with-sysroot=/
--enable-clocale=gnu
--enable-libstdcxx-debug
--enable-libstdcxx-time=yes
--enable-gnu-unique-object
--disable-vtable-verify
--enable-plugin
--with-system-zlib
--disable-browser-plugin
--enable-java-awt=gtk
--enable-gtk-cairo
--with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.9-amd64/jre
--enable-java-home
--with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.9-amd64
--with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.9-amd64
--with-arch-directory=amd64
--with-ecj-jar=/usr/share/java/eclipse-ecj.jar
--enable-objc-gc
--enable-multiarch
--with-arch-32=i586
--with-abi=m64
--with-multilib-list=m32,m64,mx32
--enable-multilib
--with-tune=generic
--enable-checking=release
--build=x86_64-linux-gnu
--host=x86_64-linux-gnu
--target=x86_64-linux-gnu
Thread model: posix
gcc version 4.9.2 (Debian 4.9.2-10)
COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.9/
:/usr/lib/gcc/x86_64-linux-gnu/4.9/
:/usr/lib/gcc/x86_64-linux-gnu/
:/usr/lib/gcc/x86_64-linux-gnu/4.9/
:/usr/lib/gcc/x86_64-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.9/32/
:/usr/lib/gcc/x86_64-linux-gnu/4.9/../../../../lib32/
:/lib/../lib32/
:/usr/lib/../lib32/
:/usr/lib/gcc/x86_64-linux-gnu/4.9/
:/usr/lib/gcc/x86_64-linux-gnu/4.9/../../../
:/lib/
:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-Wall' '-O2' '-m32' '-fshort-wchar' '-s' '-shared' '-L/usr/local/lib/' '-o' 'samp-redis.so' '-shared-libgcc' '-mtune=generic' '-march=i586'
/usr/lib/gcc/x86_64-linux-gnu/4.9/collect2
-plugin /usr/lib/gcc/x86_64-linux-gnu/4.9/liblto_plugin.so
-plugin-opt=/usr/lib/gcc/x86_64-linux-gnu/4.9/lto-wrapper
-plugin-opt=-fresolution=/tmp/ccOq8qY8.res
-plugin-opt=-pass-through=-lgcc_s
-plugin-opt=-pass-through=-lc
-plugin-opt=-pass-through=-lgcc_s
--sysroot=/
--build-id
--eh-frame-hdr
-m elf_i386
--hash-style=gnu
-shared
-o samp-redis.so
-s /usr/lib/gcc/x86_64-linux-gnu/4.9/../../../../lib32/crti.o /usr/lib/gcc/x86_64-linux-gnu/4.9/32/crtbeginS.o
-L/usr/local/lib/
-L/usr/lib/gcc/x86_64-linux-gnu/4.9/32
-L/usr/lib/gcc/x86_64-linux-gnu/4.9/../../../../lib32
-L/lib/../lib32
-L/usr/lib/../lib32
-L/usr/lib/gcc/x86_64-linux-gnu/4.9
-L/usr/lib/gcc/x86_64-linux-gnu/4.9/../../..
-Bstatic
--no-undefined
-lcpp_redis amxplugin.o amxplugin2.o main.o
-lstdc++
-lm
-lgcc_s
-lc
-lgcc_s /usr/lib/gcc/x86_64-linux-gnu/4.9/32/crtendS.o /usr/lib/gcc/x86_64-linux-gnu/4.9/../../../../lib32/crtn.o
/usr/bin/ld: cannot find
-lgcc_s
/usr/bin/ld: cannot find
-lgcc_s
collect2: error: ld returned 1 exit status
makefile:15: recipe for target 'build' failed
make: *** [build] Error 1
Apparently it can't find gcc_s whatever that is? s=static?
I tried searching for gcc libs:
$ find /usr/ -name libgcc*
/usr/lib/gcc/x86_64-linux-gnu/4.9/libgcc_s.so
/usr/lib/gcc/x86_64-linux-gnu/4.9/libgcc_eh.a
/usr/lib/gcc/x86_64-linux-gnu/4.9/x32/libgcc_s.so
/usr/lib/gcc/x86_64-linux-gnu/4.9/x32/libgcc_eh.a
/usr/lib/gcc/x86_64-linux-gnu/4.9/x32/libgcc.a
/usr/lib/gcc/x86_64-linux-gnu/4.9/libgcc.a
/usr/lib/gcc/x86_64-linux-gnu/4.9/32/libgcc_s.so
/usr/lib/gcc/x86_64-linux-gnu/4.9/32/libgcc_eh.a
/usr/lib/gcc/x86_64-linux-gnu/4.9/32/libgcc.a
/usr/lib/gcc/x86_64-linux-gnu/4.9/libgcc_s_32.so
/usr/lib/gcc/x86_64-linux-gnu/4.9/libgcc_s_x32.so
/usr/lib/gcc/x86_64-linux-gnu/4.8/libgcc_s.so
/usr/lib/gcc/x86_64-linux-gnu/4.8/libgcc_eh.a
/usr/lib/gcc/x86_64-linux-gnu/4.8/libgcc.a
/usr/lib/x86_64-linux-gnu/libgccpp.so.1
/usr/lib/x86_64-linux-gnu/libgccpp.so.1.0.3
Yet doing a ldconfig search shows files named libgcc.so.1:
$ sudo /sbin/ldconfig -p | grep libgcc
libgccpp.so.1 (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libgccpp.so.1
libgcc_s.so.1 (libc6,x32) => /usr/libx32/libgcc_s.so.1
libgcc_s.so.1 (libc6,x86-64) => /lib/x86_64-linux-gnu/libgcc_s.so.1
libgcc_s.so.1 (libc6) => /usr/lib32/libgcc_s.so.1
Could this be the reason?
And googling the error brings up some results so I'll keep digging...
Even if you build cpp_redis as static library for it to be linkable with shared library it must be compiled with -fPIC flag ie code must be relocatable. Looks like in your case linker silently ignores incompatible code when linking shared lib. You can check if symbols from cpp_redis resolved into your plugin by running nm with your .so file.
According to your update, you should link your plugin with -W,--no-undefined not cpp_redis. Most probably it will fail as symbols from host program would be missing. But you can check if symbols from cpp_redis are listed as missing or not.
What should I fix in the Makefile?
jalal#klein:~/computer_vision/mmglm_neuroimage_private-master/lib/c++/src$ make
g++ -g -Wall -Wextra -Wunreachable-code -static mmglm_spd_multithread.cpp -o ../bin/mmglm_spd_par -fopenmp -O3 -llapack -lblas -lpthread -lgfortran -lboost_system -lboost_filesystem -I ../lib/armadillo-3.930.2/include/
mmglm_spd_multithread.cpp: In function ‘int main(int, char**)’:
mmglm_spd_multithread.cpp:117:10: warning: unused variable ‘length’ [-Wunused-variable]
size_t length=0;//initializing length as a counter
^
/usr/lib/gcc/x86_64-linux-gnu/5/libgomp.a(target.o): In function `gomp_target_init':
(.text+0xba): warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/x86_64-linux-gnu/5/libgfortran.a(write.o): In function `write_float':
(.text.write_float+0xb2): undefined reference to `signbitq'
/usr/lib/gcc/x86_64-linux-gnu/5/libgfortran.a(write.o): In function `write_float':
(.text.write_float+0xbf): undefined reference to `finiteq'
/usr/lib/gcc/x86_64-linux-gnu/5/libgfortran.a(write.o): In function `write_float':
(.text.write_float+0x133): undefined reference to `quadmath_snprintf'
/usr/lib/gcc/x86_64-linux-gnu/5/libgfortran.a(write.o): In function `write_float':
(.text.write_float+0x254): undefined reference to `finiteq'
/usr/lib/gcc/x86_64-linux-gnu/5/libgfortran.a(write.o): In function `write_float':
(.text.write_float+0xaff): undefined reference to `isnanq'
/usr/lib/gcc/x86_64-linux-gnu/5/libgfortran.a(write.o): In function `write_float':
(.text.write_float+0xb83): undefined reference to `quadmath_snprintf'
/usr/lib/gcc/x86_64-linux-gnu/5/libgfortran.a(write.o): In function `write_float':
(.text.write_float+0xf7d): undefined reference to `quadmath_snprintf'
/usr/lib/gcc/x86_64-linux-gnu/5/libgfortran.a(write.o): In function `write_float':
(.text.write_float+0x10d8): undefined reference to `quadmath_snprintf'
collect2: error: ld returned 1 exit status
Makefile:13: recipe for target 'multithread' failed
make: *** [multithread] Error 1
I have:
$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 5.4.0-6ubuntu1~16.04.2' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-5 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.2)
and
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 16.04.1 LTS
Release: 16.04
Codename: xenial
Here's the related lines from Makefile:
CC= g++
CFLAGS= -Wall -Wextra -Wunreachable-code -static
#-static
#INC_PATH= -I ../lib/armadillo-7.100.3/include/
INC_PATH= -I ../lib/armadillo-3.930.2/include/
LIB_PATH= -llapack -lblas -lpthread -lgfortran -lboost_system -lboost_filesystem
#LIB_PATH= -DARMA_DONT_USE_WRAPPER
#lrefblas
all: multithread mmglm show_arma_mx armabin2asciifull armabinequaltotxt
multithread_fast:
$(CC) -g $(CFLAGS) mmglm_spd_multithread_fast.cpp -o ../bin/mmglm_spd_par_fast -fopenmp -O3 $(LIB_PATH) $(INC_PATH)
multithread:
$(CC) -g $(CFLAGS) mmglm_spd_multithread.cpp -o ../bin/mmglm_spd_par -fopenmp -O3 $(LIB_PATH) $(INC_PATH)
I even tried having -lquadmath flag but got the same error:
multithread:
$(CC) -g $(CFLAGS) mmglm_spd_multithread.cpp -o ../bin/mmglm_spd_par -fopenmp -lquadmath -O3 $(LIB_PATH) $(INC_PATH)
I also have the following packages installed:
2043 sudo apt-get install liblapack-dev
2044 sudo apt-get install liblapacke
2045 sudo apt-get install liblapacke-dev
2046 sudo apt-get install libblas-dev
2048 sudo apt-get install gfortran
2049 sudo apt-get install libatlas-base-dev
2050 sudo apt-get install liblapack3
2052 sudo apt-get install libarmadillo-dev
2056 sudo apt-get install libarmadillo6
2057 sudo apt-get install libarmadillo6-dbgsym
2071 sudo apt-get install libboost-dev
2077 sudo apt-get install libboost-system-dev
2080 sudo apt-get install libboost-filesystem-dev
Solved the problem by adding -lquadmath at the end of this line:
LIB_PATH= -llapack -lblas -lpthread -lgfortran -lboost_system -lboost_filesystem -lquadmath
I am using c++ mysql connector and when I am compiling on ubnutu 14.04 64bit (g++ 4.8.4), the following error occur:
....so: undefined reference to `get_driver_instance'
however if I try to compile it on Debian 8 64bit (g++ 4.8.4) it compile without any error...
also if I compile it on ubuntu, but with g++ 5.1 it has no errors...
The command which I run:
g++-4.9 -std=c++11 -lmysqlcppconn server.cpp -I ./websocketpp/ -L mysql-connector-c++/lib -lboost_system -lbeanstalkpp -lboost_chrono -I rapidjson/include/
EDIT:
the output of compiler with error (using -v switch)
g++ -v -std=c++11 server.cpp -I mysql-connector-c++/include/cppconn/ -I ./websocketpp/ -L mysql-connector-c++/lib -lboost_system -lmysqlcppconn -lbeanstalkpp -lboost_chrono -I rapidjson/include/ -lpthread
[1] 4364
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.8/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.8.4-2ubuntu1~14.04' --with-bugurl=file:///usr/share/doc/gcc-4.8/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.8 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.8 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-libmudflap --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.8-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04)
COLLECT_GCC_OPTIONS='-v' '-std=c++11' '-I' 'mysql-connector-c++/include/cppconn/' '-I' './websocketpp/' '-Lmysql-connector-c++/lib' '-I' 'rapidjson/include/' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
/usr/lib/gcc/x86_64-linux-gnu/4.8/cc1plus -quiet -v -I mysql-connector-c++/include/cppconn/ -I ./websocketpp/ -I rapidjson/include/ -imultiarch x86_64-linux-gnu -D_GNU_SOURCE server.cpp -quiet -dumpbase server.cpp -mtune=generic -march=x86-64 -auxbase server -std=c++11 -version -fstack-protector -Wformat -Wformat-security -o /tmp/cc8ZjcCL.s
GNU C++ (Ubuntu 4.8.4-2ubuntu1~14.04) version 4.8.4 (x86_64-linux-gnu)
compiled by GNU C version 4.8.4, GMP version 5.1.3, MPFR version 3.1.2-p3, MPC version 1.0.1
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring duplicate directory "/usr/include/x86_64-linux-gnu/c++/4.8"
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../x86_64-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
mysql-connector-c++/include/cppconn/
./websocketpp/
rapidjson/include/
/usr/include/c++/4.8
/usr/include/x86_64-linux-gnu/c++/4.8
/usr/include/c++/4.8/backward
/usr/lib/gcc/x86_64-linux-gnu/4.8/include
/usr/local/include
/usr/lib/gcc/x86_64-linux-gnu/4.8/include-fixed
/usr/include/x86_64-linux-gnu
/usr/include
End of search list.
GNU C++ (Ubuntu 4.8.4-2ubuntu1~14.04) version 4.8.4 (x86_64-linux-gnu)
compiled by GNU C version 4.8.4, GMP version 5.1.3, MPFR version 3.1.2-p3, MPC version 1.0.1
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 9cb21c615435621dce26fe5e793192fd
COLLECT_GCC_OPTIONS='-v' '-std=c++11' '-I' 'mysql-connector-c++/include/cppconn/' '-I' './websocketpp/' '-Lmysql-connector-c++/lib' '-I' 'rapidjson/include/' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
as -v -I mysql-connector-c++/include/cppconn/ -I ./websocketpp/ -I rapidjson/include/ --64 -o /tmp/cczJ3S6r.o /tmp/cc8ZjcCL.s
GNU assembler version 2.24 (x86_64-linux-gnu) using BFD version (GNU Binutils for Ubuntu) 2.24
COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.8/:/usr/lib/gcc/x86_64-linux-gnu/4.8/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/4.8/:/usr/lib/gcc/x86_64-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.8/:/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/:/lib/x86_64-linux-gnu/:/lib/../lib/:/usr/lib/x86_64-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-std=c++11' '-I' 'mysql-connector-c++/include/cppconn/' '-I' './websocketpp/' '-Lmysql-connector-c++/lib' '-I' 'rapidjson/include/' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
/usr/lib/gcc/x86_64-linux-gnu/4.8/collect2 --sysroot=/ --build-id --eh-frame-hdr -m elf_x86_64 --hash-style=gnu --as-needed -dynamic-linker /lib64/ld-linux-x86-64.so.2 -z relro /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/4.8/crtbegin.o -Lmysql-connector-c++/lib -L/usr/lib/gcc/x86_64-linux-gnu/4.8 -L/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib -L/lib/x86_64-linux-gnu -L/lib/../lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/4.8/../../.. /tmp/cczJ3S6r.o -lboost_system -lmysqlcppconn -lbeanstalkpp -lboost_chrono -lpthread -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/lib/gcc/x86_64-linux-gnu/4.8/crtend.o /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crtn.o
//usr/local/lib/libbeanstalkpp.so: undefined reference to `get_driver_instance'
collect2: error: ld returned 1 exit status
[1]+ Exit 1 g++ -v -std=c++11 server.cpp -I mysql-connector-c++/include/cppconn/ -I ./websocketpp/ -L mysql-connector-c++/lib -lboost_system -lmysqlcppconn -lbeanstalkpp -lboost_chrono -I rapidjson/include/ -lpthread
I'm trying to link the output of C++ using ld and not g++. I'm only doing this to learn how to do it, not for practical purposes, so please don't suggest just to do it with g++.
Looking at this question, the person gets the same error when they run the ld command:
$ ld test.o -o test.out
ld: warning: cannot find entry symbol _start; defaulting to 00000000004000e8
test.o: In function `main':
test.cpp:(.text+0x1c): undefined reference to `strcasecmp'
test.cpp:(.text+0x23): undefined reference to `std::cout'
test.cpp:(.text+0x28): undefined reference to `std::ostream::operator<<(int)'
test.cpp:(.text+0x2d): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
test.cpp:(.text+0x35): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
test.o: In function `__static_initialization_and_destruction_0(int, int)':
test.cpp:(.text+0x75): undefined reference to `std::ios_base::Init::Init()'
test.cpp:(.text+0x7a): undefined reference to `__dso_handle'
test.cpp:(.text+0x84): undefined reference to `std::ios_base::Init::~Init()'
test.cpp:(.text+0x89): undefined reference to `__cxa_atexit'
ld: test.out: hidden symbol `__dso_handle' isn't defined
ld: final link failed: Bad value
The answers in the linked post suggest that adding the C++ library as a linker argument will fix the problem, so I tried
ld test.o -o test.out -llibstd++
which is what they suggested, and I also tried a lot of other library names like libstdc++ or stdc++. But I'll always get an error that looks like
ld: cannot find -llibstd++
What am I doing wrong and how can I link my object files using ld?
If you run g++ with the -v flag, you'll see the link line it uses. Here's a simple example program:
#include <iostream>
int main(void)
{
std::cout << "Hello, world!" << std::endl;
return 0;
}
And the output from running g++ -v -o example example.cpp:
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.4.4-14ubuntu5.1' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.4 --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.4.5 (Ubuntu/Linaro 4.4.4-14ubuntu5.1)
COLLECT_GCC_OPTIONS='-v' '-o' 'example' '-shared-libgcc' '-mtune=generic'
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/cc1plus -quiet -v -D_GNU_SOURCE example.cpp -D_FORTIFY_SOURCE=2 -quiet -dumpbase example.cpp -mtune=generic -auxbase example -version -fstack-protector -o /tmp/ccV8qjvd.s
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../../x86_64-linux-gnu/include"
ignoring nonexistent directory "/usr/include/x86_64-linux-gnu"
#include "..." search starts here:
#include <...> search starts here:
/usr/include/c++/4.4
/usr/include/c++/4.4/x86_64-linux-gnu
/usr/include/c++/4.4/backward
/usr/local/include
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/include
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/include-fixed
/usr/include
End of search list.
GNU C++ (Ubuntu/Linaro 4.4.4-14ubuntu5.1) version 4.4.5 (x86_64-linux-gnu)
compiled by GNU C version 4.4.5, GMP version 4.3.2, MPFR version 3.0.0-p3.
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: d92fbc2d715a3b7e0f4133f0c40053e4
COLLECT_GCC_OPTIONS='-v' '-o' 'example' '-shared-libgcc' '-mtune=generic'
as -V -Qy -o /tmp/ccGHR0pc.o /tmp/ccV8qjvd.s
GNU assembler version 2.20.51 (x86_64-linux-gnu) using BFD version (GNU Binutils for Ubuntu) 2.20.51-system.20100908
COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.4.5/:/usr/lib/gcc/x86_64-linux-gnu/4.4.5/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/4.4.5/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/4.4.5/:/usr/lib/gcc/x86_64-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.4.5/:/usr/lib/gcc/x86_64-linux-gnu/4.4.5/:/usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../../lib/:/lib/../lib/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../:/lib/:/usr/lib/:/usr/lib/x86_64-linux-gnu/
COLLECT_GCC_OPTIONS='-v' '-o' 'example' '-shared-libgcc' '-mtune=generic'
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/collect2 --build-id --eh-frame-hdr -m elf_x86_64 --hash-style=gnu -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o example -z relro /usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../../lib/crt1.o /usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../../lib/crti.o /usr/lib/gcc/x86_64-linux-gnu/4.4.5/crtbegin.o -L/usr/lib/gcc/x86_64-linux-gnu/4.4.5 -L/usr/lib/gcc/x86_64-linux-gnu/4.4.5 -L/usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../../lib -L/lib/../lib -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../.. -L/usr/lib/x86_64-linux-gnu /tmp/ccGHR0pc.o -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/lib/gcc/x86_64-linux-gnu/4.4.5/crtend.o /usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../../lib/crtn.o
Wow, what a mess. Conveniently the link line is the last one there, so you can see what's happening pretty easily.
As you noticed in your comment below, the front-end is using collect2 rather than ld. Luckily, collect2 is just an alias for ld. Here's an example using it:
First let's generate an object file:
$ ls
example.cpp
$ c++ -c example.cpp
$ ls
example.cpp example.o
Then we'll use the front-end to link it to see the link line:
$ c++ -v -o example example.o
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.4.4-14ubuntu5.1' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.4 --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.4.5 (Ubuntu/Linaro 4.4.4-14ubuntu5.1)
COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.4.5/:/usr/lib/gcc/x86_64-linux-gnu/4.4.5/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/4.4.5/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/4.4.5/:/usr/lib/gcc/x86_64-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.4.5/:/usr/lib/gcc/x86_64-linux-gnu/4.4.5/:/usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../../lib/:/lib/../lib/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../:/lib/:/usr/lib/:/usr/lib/x86_64-linux-gnu/
COLLECT_GCC_OPTIONS='-v' '-o' 'example' '-shared-libgcc' '-mtune=generic'
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/collect2 --build-id --eh-frame-hdr -m elf_x86_64 --hash-style=gnu -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o example -z relro /usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../../lib/crt1.o /usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../../lib/crti.o /usr/lib/gcc/x86_64-linux-gnu/4.4.5/crtbegin.o -L/usr/lib/gcc/x86_64-linux-gnu/4.4.5 -L/usr/lib/gcc/x86_64-linux-gnu/4.4.5 -L/usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../../lib -L/lib/../lib -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../.. -L/usr/lib/x86_64-linux-gnu example.o -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/lib/gcc/x86_64-linux-gnu/4.4.5/crtend.o /usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../../lib/crtn.o
Then throw away the binary, and link ourselves (normally, I would have just copy/pasted the line, but to make it easier to read I did it the multiline way with \s):
$ ls
example example.cpp example.o
$ rm example
$ ls
example.cpp example.o
$ ld \
> --build-id \
> --eh-frame-hdr \
> -m elf_x86_64 \
> --hash-style=gnu \
> -dynamic-linker \
> /lib64/ld-linux-x86-64.so.2 \
> -o example \
> -z relro \
> /usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../../lib/crt1.o \
> /usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../../lib/crti.o \
> /usr/lib/gcc/x86_64-linux-gnu/4.4.5/crtbegin.o \
> -L/usr/lib/gcc/x86_64-linux-gnu/4.4.5 \
> -L/usr/lib/gcc/x86_64-linux-gnu/4.4.5 \
> -L/usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../../lib \
> -L/lib/../lib \
> -L/usr/lib/../lib \
> -L/usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../.. \
> -L/usr/lib/x86_64-linux-gnu \
> example.o \
> -lstdc++ \
> -lm \
> -lgcc_s \
> -lgcc \
> -lc \
> -lgcc_s \
> -lgcc \
> /usr/lib/gcc/x86_64-linux-gnu/4.4.5/crtend.o \
> /usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../../lib/crtn.o
Finally, run it!
$ ls
example example.cpp example.o
$ ./example
Hello, world!
You can probably significantly shorten that link line by removing some arguments. Here's the minimal set I came up with after some experimentation:
$ ld \
> -dynamic-linker \
> /lib64/ld-linux-x86-64.so.2 \
> -o example \
> /usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../../lib/crt1.o \
> /usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../../lib/crti.o \
> /usr/lib/gcc/x86_64-linux-gnu/4.4.5/crtbegin.o \
> example.o \
> -L/usr/lib/gcc/x86_64-linux-gnu/4.4.5 \
> -lstdc++ \
> -lc \
> /usr/lib/gcc/x86_64-linux-gnu/4.4.5/crtend.o \
> /usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../../lib/crtn.o
This set of flags and libraries will of course depend on what library functions and language features your program uses.
I managed to compile like this
ld -s hello.o crt2.o -o hello.exe libstdc++.a libgcc.a libmingw32.a libmingwex.a libmsvcrt.a libkernel32.a