unexplained linker error - c++

I have the following files on my computer.
/lib64/libm.so.6
/usr/lib64/libm.so
but when I link with the following, I get errors
$ g++ -L/usr/lib64 -O3 -static -Wall -Wno-char-subscripts -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -IMSToolkit/include Comet.o Threading.o CometSearch.o CometPreprocess.o CometPostAnalysis.o CometMassSpecUtils.o CometWriteOut.o CometWriteSqt.o CometWritePepXML.o -LMSToolkit -lmstoolkitlite -o comet.exe
/usr/bin/ld: cannot find -lm
collect2: ld returned 1 exit status
$
Does anyone knows why?

Yes I do. You are trying to link a static exe with no shared libraries - the libraries you have are shared because of the .so extension - you need to install libm.a
Either remove the -static option or install the .a versions of the library

Related

linking failed with undefined reference to libboost_thread

I am compiling an opensource project to run on my machine which is this project. It requires boost library so I installed the Boost_1_55 library on my ubuntu machine but the compiling process was not successfully finished by printing out some error messages as follows.
libtool: link: g++ -g -O3 -Wall -DKENLM_MAX_ORDER=6 -W -Wall -Wno-sign-compare -I./.. -pthread -I/usr/include -g -O2 -o .libs/query query_main.o ./.libs/libklm.so ../util/.libs/libklm_util.so -lz -L/usr/lib/x86_64-linux-gnu -lboost_program_options -lboost_thread -lboost_system -lpthread -lrt -pthread
../util/.libs/libklm_util.so: undefined reference to `boost::thread::join()'
../util/.libs/libklm_util.so: undefined reference to `boost::thread::~thread()'
./.libs/libklm.so: undefined reference to `boost::thread::start_thread()'
collect2: ld returned 1 exit status
This answer seems the solution for my problem but the result of ls -al /usr/local/lib | grep thread showed me like below.
libboost_thread.a
libboost_thread.so -> libboost_thread.so.1.55.0
libboost_thread.so.1.49.0
libboost_thread.so.1.55.0
I don't know what else to check more. Thank you in advance for your help.
You can try to add /usr/local/lib to LD_LIBRARY_PATH like this
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
You have the static Boost library object (libboost_thread.so) but do you have the Boost development files installed? Check to see if the /usr/include/boost/thread directory exists and has *.hpp files in it. If not you may need to install the libboost-thread-dev package for your distribution or download the header files directly from Boost.org.

Static compilation fails: dynamic STT_GNU_IFUNC symbol `tan' .. can not be used when making an executable

I'm trying to compile expect statically on my PC, but I seem to run into some strange compiling errors.
I was able to compile tcl statically like this:
./configure --disable-load --disable-shared
Then add to the makefile CFLAGS variable -static keyword. Now after compiling I seem to have a static tcl, and a library:
-rw-rw-r-- 1 jlumme jlumme 3284332 loka 2 11:27 libtcl8.6.a
After this, I configure expect:
./configure --with-tclconfig=${TCL_DIR}/unix --disable-shared --disable-load
And then compile (first dynamic):
$ gcc -pipe -O2 -fomit-frame-pointer -Wall -Wl,--export-dynamic -o expect exp_main_exp.o -L/home/jlumme/Downloads/compile/expect5.45 -lexpect5.45 -L/home/jlumme/Downloads/compile/tcl8.6.4/unix -ltcl8.6 -lieee -lm -lpthread -lz
The result looks ok:
$ file expect
expect: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=a613c260944117f43bcaebc43c51c421dce273a7, not stripped
But now if I want to attempt a static compilation, I get errors that I dont understand:
gcc -pipe -O2 -fomit-frame-pointer -Wall -Wl,--export-dynamic -o expect exp_main_exp.o -L/home/jlumme/Downloads/compile/expect5.45 -lexpect5.45 -L/home/jlumme/Downloads/compile/tcl8.6.4/unix -ltcl8.6 -lieee -lm -lpthread -lz -static
/home/jlumme/Downloads/compile/tcl8.6.4/unix/libtcl8.6.a(tclUnixCompat.o): In function `TclpGetGrGid':
tclUnixCompat.c:(.text+0x41c): warning: Using 'getgrgid_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/home/jlumme/Downloads/compile/tcl8.6.4/unix/libtcl8.6.a(tclUnixCompat.o): In function `TclpGetGrNam':
tclUnixCompat.c:(.text+0x31c): warning: Using 'getgrnam_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
<HANDFUL OF WARNINGS REMOVED>
/usr/bin/ld: dynamic STT_GNU_IFUNC symbol `tan' with pointer equality in `/usr/lib/gcc/x86_64-linux-gnu/4.9/../../../x86_64-linux-gnu/libm.a(s_tan.o)' can not be used when making an executable; recompile with -fPIE and relink with -pie
collect2: error: ld returned 1 exit status
I dont really understand what this final linking problem is, can someone help ?

Play MP3 file in NaCl

Following the NACI guide to build the SDK examples worked perfectly.
Now I'm going to build my own shared library to practice.
I used the following command to compile eightball.cc into eightball_x86_32.o with no problem.
i686-nacl-g++ -o eightball_x86_32.o -c eightball.cc -m32 -g -O0 -pthread -std=gnu++98 -Wno-long-long -Wall -fPIC
But when I want to link eightball_x86_32.o into eightball_x86_32.so, the compiler says cannot find -lppapi_cpp.
i686-nacl-g++ -o libeightball.so eightball_x86_32.o -m32 -g -ldl -lppapi_cpp -lppapi -shared
/Users/Shih/nacl_sdk/pepper_29/toolchain/mac_x86_glibc/bin/../lib/gcc/x86_64-nacl/4.4.3/../../../../x86_64-nacl/bin/ld: cannot find -lppapi_cpp
collect2: ld returned 1 exit status
The ppapi_cpp library and other higher level libraries such as nacl_io
live under $NACL_SDK_ROOT/lib. You need to add the requisite library
path to your link command (e.g. -L$NACL_SDK_ROOT/lib/glibc_x86_32/Debug).

C++: linker cannot find -lcrypto, but the library is in the path

I am compiling a C++ application using GNU g++. The project takes advantage of OpenSSL libraries.
Background
On my machine (a 64 bit CentOS quad core) I compile and link my files.
g++ -g -c -L/usr/local/lib/ -L/usr/lib64/
-I/usr/local/include/ -I/usr/local/ssl/include/
-lcrypto mysrc1.cpp mysrc2.cpp mysrc3.cpp
g++ -L/usr/local/lib/ -L/usr/lib64/ -lcrypto
*.o -o ./myapp.out
My application uses function MD5 which is contained in libcrypto.so. As you can see I specify to g++ the dirs where to search using the -L, -I options and which libraries to look for with the -l<lib-name> option. There are some trivial paths like /usr/local/lib which can be omitted of course, but I specified them because the makefile is parametric.
The problem
My problem is that I can successfully compile my stuff (first command), but linking fails (second command):
/usr/bin/ld: cannot find -lcrypto
collect2: ld returned 1 exit status
make: * [cppsims_par] Error 1
But I did check folders and everything... libcrypto.so is inside /usr/lib64/. What is going on?
It may help if you try strace to find why it failed the file lookup
strace -f -e trace=file g++ -L/usr/local/lib/ -L/usr/lib64/ -lcrypto
*.o -o ./myapp.out
I did find the problem and it is related to this question: ld cannot find an existing library
Actually I had no symlink libcrypto.so and the compiler was not able to find the library...
I had related issue, and resolved it after inspecting the trace.
I had
-L<my/path/to/lib> -llib_some_library
when it should have been
-L<my/path/to/lib> -lsome_library

How to tell ld where to find libc

So I created a static library with some basic assistance functions in C++ today. I built it with Clang 3.2 (SVN snapshot). However when I try to run a test program that links to it (prog.cpp), I get the following error:
~/Projects/CPP/AssisterLib> g++ prog.cpp -o program -static -L. -lassister
/usr/lib64/gcc/x86_64-suse-linux/4.6/../../../../x86_64-suse-linux/bin/ld: cannot find -lm
/usr/lib64/gcc/x86_64-suse-linux/4.6/../../../../x86_64-suse-linux/bin/ld: cannot find -lc
collect2: ld returned 1 exit status
I get the same error with G++ and Clang++. Apparently it can't find libc.a and libm.a, which are both in /usr/lib64 (provided by glibc-devel in OpenSUSE). Adding -L/usr/lib64 does nothing for the error.
Why can't ld find those libraries? Is there a flag that I'm missing?
The problem is likely the use of -static. I would conclude you do not have static version of the libm and libc installed. You can try removing -static to confirm.
The -static flag signals to the compiler that you want your executable to be entirely statically linked, and so it fails if not all the libraries have static versions available.