Problems when linking shared library with GCC [duplicate] - c++

This question already has an answer here:
Libraries in /usr/local/lib not found
(1 answer)
Closed 2 years ago.
I try to link a shared library just installed on my system, but for some reason the output doesn't want to execute. This is my compilation:
gcc -o test test.c -lgpio
When running the output, I get the following error:
./test: error while loading shared libraries: libgpio-3.0.0.so.3: cannot open shared object file: No such file or directory
Which is weird since I have this exact file in my /usr/local/lib-folder (along with libgpio-3.0.0.so and the other shared library files). I don't have much experience with GCC, so can someone please explain what went wrong?

Your library folder is probably not in the default search path. You'll need to specify it using the -L option when you compile:
gcc -L /usr/local/lib-folder -o test test.c -lgpio
You'll also need to add this folder to the LD_LIBRARY_PATH environment variable when you run the program.

Related

How do I link this dynamic-link library to the program? [duplicate]

This question already has answers here:
gcc/g++: "No such file or directory"
(3 answers)
Closed 3 years ago.
I have exported
/home/username/mesa/lib
LD_LIBRARY_PATH and tried to link the libraries, but I do not know what I have type wrong to compile the program.
So I tried to compile testing.cpp with the g++ command and it says :
fatal error: osmesa.h: No such file or directory
#include <osmesa.h>
I suppose I have typed wrong the command.
The command I tried: g++ testing.cpp -L/home/username/mesa/lib/libOSMesa.so -lmesa -s -Lmesa -lOSMesa -lGLU
Source code of testin.cpp:
#include <osmesa.h>
int main()
{
return 0;
}
libraries in side /home/username/mesa/lib:
libOSMesa.la libOSMesa.so libOSMesa.so.8 libOSMesa.so.8.0.0
You must pass the include directories as well, use the -I compiler option.
This is because the compiler will by default not look for headers in your home directory (it will do that for system installed libraries in /usr/include).

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.

Undefined reference to PQfinish even though libraries etc. are included [duplicate]

This question already has answers here:
Why does the order in which libraries are linked sometimes cause errors in GCC?
(9 answers)
Closed 8 years ago.
I'm trying to compile a C++ application using libpq.
On my Debian it compiles fine with the following command:
g++ -I /usr/include/postgresql/ -L /usr/lib/postgresql/9.3/lib/ -lpq -o test test.cpp
On my Ubuntu I get the following error:
undefined reference to 'PQfinish'
I've included link to postgresql headers and libraries and used the -lpq. What am I missing?
Move -lpq to the end of the command line.
Linking is done in the order specified, and objects are only taken from libraries if they are needed to resolve symbols at that point. So each library has to come after any other object that needs it.

c/c++ boost - problem compiling

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.