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
Related
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
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.
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 downloaded the pre built headers and libraries for Intel TBB from Intel's TBB website. I updated ldconfig to add the tbb shared library to /usr/lib. However on compiling with -tbb the code using g++ ld returns -1 as it is unable to find libtbb
libtbb.so.2 is present in /usr/lib/
here is my full g++ output
g++ hellotbb.cpp -o htbb -I$HOME/libs/tbb43/include -v -ltbb
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.2-19ubuntu1' --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.2 (Ubuntu 4.8.2-19ubuntu1)
COLLECT_GCC_OPTIONS='-o' 'htbb' '-I' '/home/govind/libs/tbb43/include' '-v' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
/usr/lib/gcc/x86_64-linux-gnu/4.8/cc1plus -quiet -v -I /home/govind/libs/tbb43/include -imultiarch x86_64-linux-gnu -D_GNU_SOURCE hellotbb.cpp -quiet -dumpbase hellotbb.cpp -mtune=generic -march=x86-64 -auxbase hellotbb -version -fstack-protector -Wformat -Wformat-security -o /tmp/ccnjmEt3.s
GNU C++ (Ubuntu 4.8.2-19ubuntu1) version 4.8.2 (x86_64-linux-gnu)
compiled by GNU C version 4.8.2, 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:
/home/govind/libs/tbb43/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.2-19ubuntu1) version 4.8.2 (x86_64-linux-gnu)
compiled by GNU C version 4.8.2, 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: 26a7c0bd346d04102f6aea776e0cccc5
COLLECT_GCC_OPTIONS='-o' 'htbb' '-I' '/home/govind/libs/tbb43/include' '-v' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
as -v -I /home/govind/libs/tbb43/include --64 -o /tmp/ccC9Xpee.o /tmp/ccnjmEt3.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='-o' 'htbb' '-I' '/home/govind/libs/tbb43/include' '-v' '-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 -o htbb /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 -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/ccC9Xpee.o -ltbb -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/bin/ld: cannot find -ltbb
collect2: error: ld returned 1 exit status
It seems that ld does not detect my tbb library in /usr/lib/
Make sure libtbb.so is also present and install libtbb-dev package if needed:
apt-get install libtbb-dev
BTW, your include files are located in suspicious place. Looks like you mixed installation of TBB binaries and include files from different sources. It can lead to bad problems especially if headers are newer than binaries.