c/c++ boost - problem compiling - c++

I've been pulling my hair out trying to figure out how to compile my app with boost regex.
I've installed boost from source on centos 5.
g++ -lboost_regex -o my_app my_app.c $(mysql_config --libs --cflags)
It compiles without any errors, however when I execute it:
error while loading shared libraries: libboost_regex.so.1.46.1: cannot open shared object file: No such file or directory
The location of that file is:
/usr/local/lib/libboost_regex.so.1.46.1
Anyone experience the same issues?

Did you try LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH your_program to make sure it knows where to find the shared object? You can set the path when you link by using -Wl,-R/usr/local/lib.
EDIT: To be more clear, when you link your code the linker will embed an RPATH and RUNPATH into the binary. These values tell the runtime loader where to find required shared objects.
If you add -Wl,-R/usr/local/lib to your link command that should cause it to embed that directory and always check it when loading your program.

Try this.
$ LD_LIBRARY_PATH=/usr/local/lib:LD_LIBRARY_PATH
$ export LD_LIBRARY PATH
Now try and tell us what happens.

Related

C++ Executable cannot find library at runtime, even though it's in /usr/lib (On Linux)

I'm creating a game in c++ with the Panda3D framework on Ubuntu. All of the Panda3D shared libraries are in /usr/lib/panda3d and all of the headers are in /usr/include/panda3d. I'm compiling with SCons, but I've tried it with gcc and it's the same, so here are the commands:
g++ -o src/main.o -c -fPIC -O2 -std=gnu++11 -I/usr/include/python2.7 -I/usr/include/panda3d -Iinclude src/main.cpp
g++ -o Test src/main.o -L/usr/lib/panda3d -lp3framework -lpanda -lpandafx -lpandaexpress -lpandabullet -lp3dtoolconfig -lp3dtool -lp3direct -lpthread
And here is the error I get when I run the executable:
./Test: error while loading shared libraries: libp3framework.so.1.11: cannot open shared object file: No such file or directory
There isn't much in the code, just initializing a Panda3D window, so I doubt that's the culprit.
As I said before, the libraries are in /usr/lib/panda3d, I've checked about a million times now and it's driving me crazy. I can't think of a single reason why I would get this error. Any help is appreciated :)
Edit:
I was looking through my files and there's a panda3d.conf file in /etc/ld.so.conf.d with one line: /usr/local/lib/x86_64-linux-gnu/panda3d. Does this have anything to do with it?
Edit #2:
I used the path in the above edit as the library path and got the same results, unfortunately.
I can't think of a single reason why I would get this error.
The reason is very simple: the dynamic loader hasn't been told to look in /usr/lib/panda3d for shared libraries, and so doesn't.
You can run your program with:
LD_DEBUG=files,libs ./Test
and observe which directories the loader is searching.
panda3d.conf ... with one line: /usr/local/lib/x86_64-linux-gnu/panda3d
That is the wrong directory (or at least not the one where your libraries are).
One way to fix this is to correct the above path to /usr/lib/panda3d and run sudo ldconfig.
Another way is to add -Wl,-rpath=/usr/lib/panda3d to your link line.

A simpler method to load shared libraries without root

I am trying to compile and run a C++ program on a server where I don't have root access. I am having trouble linkingboost_iostreams library.
I can successfully compile my program by pointing to the boost installation directory using the -L flag as:
g++ -I path/to/boost/build/include -o out prog1.cpp prog2.cpp -L path/to/boost/build/lib -lboost_iostreams
However, if I run the program as ./out I get the error error while loading shared libraries: libboost_iostreams.so.1.67.0: cannot open shared object file: No such file or directory since the linker is not able to locate libboost_iostreams.so.1.67.0 (which DOES exist under path/to/boost/build/lib)
Thanks to this answer, I was able to explicitly specify LD_LIBRARY_PATH and run the program as
LD_LIBRARY_PATH="path/to/boost/build/lib" ./out.
Since I am not root, I can't run ldconfig either. I was wondering if there is a way to load dynamic libraries without having to prefix LD_LIBRARY_PATH when you run the program and no root access.
I have figured out a way to solve this using the method explained here https://amir.rachum.com/blog/2016/09/17/shared-libraries/. The solution is to use rpath during compilation.
According to the article The only difference between rpath and
runpath is the order they are searched in. Specifically, their
relation to LD_LIBRARY_PATH - rpath is searched in before
LD_LIBRARY_PATH while runpath is searched in after. The meaning of
this is that rpath cannot be changed dynamically with environment
variables while runpath can.
In short once you compile with -rpath path/to/boost/build/lib, the directory containing the library libboost_iostreams.so.1.67.0 is searched at runtime without having to prefix LD_LIBRARY_PATH="path/to/boost/build/lib" ./out.
After compiling with
g++ -I path/to/boost/build/include -o out prog1.cpp prog2.cpp -L path/to/boost/build/lib -lboost_iostreams -rpath path/to/boost/build/lib
I was able to run ./out without any issues.
EDIT 1
As pointed by Nikos in the comments, alternatively you can set your LD_LIBRARY_PATH by export LD_LIBRARY_PATH=path/to/boost/build/lib. Add this line to .~/.bashrc file so that it is not lost when you log out.

Can't link libboost solution on running time

I installed boost_1_55_0 under /usr/local/boost_1_55_0, and in my makefile I set the linking flag as:
CC = g++
CFLAGS = -I ./ -I/usr/local/boost_1_55_0 -w -std=c++11
LFLAGS = -L./ -lm -lpthread -L/usr/local/boost_1_55_0/lib -lboost_regex
I encountered nothing during compile time, but I got:
./$(exec): error while loading shared libraries:
libboost_regex.so.1.55.0: cannot open shared object file: No such file
or directory
I'm pretty sure I cleaned all old files and compiled again, I also granted access to read the files in usr/local/boost_1_55_0 by sudo chmod -R +x /usr/local/boost_1_55_0 just in case I banned access to read. However, I still got this error. I also tried to move boost_1_55_0 in /usr/lib/ but it does not work either.
I've also read this question: Compiling issue with boost and tried both solutions under this question, but neither of them works for me. Can someone figure out a different solution?
As the error implies, your executable is unable to find the shared library. Add the .so file (or all of them, if you want to be safe) that was generated by the boost build process to the same directory where your program's executable is getting generated, and try running the program again.
I manually cleaned the executable files and compiled it again, and it somehow solved my problem. I am guessing there must be some config in my system messed up so that the executable would invisibly linked to some outdated directory and cause this problem.

Executing cross-compiled C++ program using Boost on Raspberry Pi

I have built a GCC cross toolchain for the RPi and can cross-compile C++ source and successfully run it after copying the executable to the RPi.
Next I built the Boost libraries targeting ARM, using the cross toolchain. I can successfully build and link C++ source to those Boost libraries using the cross toolchain on my PC.
I then copied the program, dynamically linked to Boost, to the RPi and copied all built libraries into /usr/local/lib on the Pi. However, executing fails:
$ ./my_program
./my_program: error while loading shared libraries: libboost_system.so.1.60.0: cannot open shared object file: No such file or directory
Again, this library, libboost_system.so.1.60.0, exists in /usr/local/lib.
I also tried
export LD_LIBRARY_PATH='/usr/local/lib'
but that doesn't change anything. What am I doing wrong?
EDIT:
I build all source files like this (rpi-g++ is a symlink to my cross-compiler):
rpi-g++ -c -std=c++1y -Wall -Wextra -pedantic -O2 -I /path/to/cross/boost/include *.cpp
rpi-g++ -o myprog *.o -L /path/to/cross/boost/lib/ -lboost_system -pthread
EDIT 2:
When linked with
rpi-g++ -o myprog *.o -L /path/to/cross/boost/lib/ -rdynamic -lboost_system -pthread
the problem remains the same. I have checked and verified everything suggested by Technaton as well. Strangely, ldd insists that the created executable is "not a dynamic executable" (checked that on my PC and on the RPi), which doesn't make sense to me.
There are several things you can check. I've posted a complete check list here, but judging from your linker command line, number 5 is probably the culprit.
Check that your library and your program are correctly build for the target architecture. You can verify that by using file ./myprog and file libboost_system.so.1.60.0.
Make sure that you have copied the actual shared object, and not a link to it.
Ensure that the shared object file's permissions are sane (0755).
Run ldconfig -v and check that your shared object file is picked up. Normally, /usr/local/lib is in the standard library search path, and LD_LIBRARY_PATH is not required.
Make sure that your program is actually dynamically linked by running ldd ./myprog. Judging from your linker command line, that is the problem: You're missing -rdynamic.
Check the paths returned from ldd: If you have linked with rpath, the library search path might be screwed up. Try again without -rpath.

Boost library missing at runtime (Can compile. Can't run)

This is a pretty entry level question but I just can't seem to find any relevant answers.
I can compile this no problem using the following command:
g++ client.cpp -o client -lboost_system -lpthread
When I try to run the program I get the following error:
./client
./client: error while loading shared libraries: libboost_system.so.1.58.0: cannot open shared object file: No such file or directory
this file exists: /usr/local/lib/libboost_system.so
What am I doing wrong here?
You have to set the path where to find the shared object, the command LD_LIBRARY_PATH=path where shared object is will do it. I guess file is somewhere otherwise link would fail.
It should be some linking error,
Try to compile it and create the .o file first.
g++ -C .cpp // here you need to mention the includes from library. -I
And then create the executable,
g++ -O // here you need to mention the path of the library. -l
Incase your prog is correct, it will create the exe else it will show you the error. Incase of linking error, try to include the .h and lib path correctly in the program.
I didn't get a chance to test your answers. Upgraded to ubuntu 15.04 while troubleshooting another unrelated problem which happened to fix this problem aswell.
Thanks anyway.