Shared object not found but is present in the linker directory - c++

I have downloaded and build boost_1_68_0 from source and got the following message:
The Boost C++ Libraries were successfully built!
The following directory should be added to compiler include paths:
/resources/boost_1_68_0
The following directory should be added to linker library paths:
/resources/boost_1_68_0/stage/lib
The code compiles fine with this:
g++-4.9 -std=c++11 -I /resources/boost_1_68_0 -L /resources/boost_1_68_0/stage/lib regex.cpp -lboost_regex -o reg
When I'm trying to run the code I get this:
./reg: error while loading shared libraries: libboost_regex.so.1.68.0:
cannot open shared object file: No such file or directory
However, inside /resources/boost_1_68_0/stage/lib I do have the following files:
libboost_regex.so.1.68.0
libboost_regex.so -> libboost_regex.so.1.68.0
Is there a way to make it look for the shared object inside the /resources/boost_1_68_0/stage/lib direcotory?
Thank you!
Using CentOS 7

Assuming you have root access on the machine, try:
echo "/resources/boost_1_68_0/stage/lib" >> /etc/ld.so.conf.d/boost.conf
and re-run ldconfig.

You have to put /resources/boost_1_68_0/stage/lib in LD_LIBRARY_PATH:
export LD_LIBRARY_PATH=/resources/boost_1_68_0/stage/lib:${LD_LIBRARY_PATH}

Related

Compile mex function with external libraries

I'm trying to generate a mex function usigin external libraries. I'm using Ubuntu 18 and Matlab R2021a.
In particular I want to compile my file.cpp that uses my cpp library called model.
What I did is
mex -I<path_library_include> -L<path_library_so_file> -lmodel.so -lboost_system -lstdc++ file.cpp -v
where in -I i put the path where is the include of the library in -L the path in which the libmodel.so is located, then I added 2 more libraries and at the end the source file that I want to compile.
In this way I can compile my source but when I try to execute the mex function I get:
libmodel.so: cannot open shared object file: No such file or directory
I also tested the library outside matlab and works fine, this is the command that I use to compile the library outside Matlab
gcc -Wall -I<path_library_include> -L<path_library_so_file> main.cpp -lmodel -lboost_system -lstdc++ -o main
What could be the problem with Matlab?
Thanks to 273K that gave me the right direction.
The problem was that the LD_LIBRARY_PATH was not configured well in fact running /sbin/ldconfig -v my library was not present. So to add the shared library i created a new file as root in /etc/ld.so.conf.d/ called mylib.conf it is not important the name just the extension. Then I run
sudo ldconfig
after that the library was present in fact running
/sbin/ldconfig -v | grep model
where model is the name of my library. it is possible to see the output.

libopencv_imgcodecs.so.3.2: cannot open shared object file: No such file or directory

I know that a similar question has been asked before, but none of the suggestions have helped.
I am trying to compile an OpenCV project using C++ in Ubuntu 15.10. I can run the project correctly in Netbeans. But I am supposed to send this to someone who will be using the command line. I can compile the program with the line:
g++ -ggdb -o convert *.cpp \`pkg-config --cflags --libs opencv\`
Where convert is the chosen name for the executable. There are no problems after executing this line. But when I run
./convert "image1.tif" "image2.tif"
I get:
./convert: error while loading shared libraries: libopencv_imgcodecs.so.3.2: cannot
open shared object file: No such file or directory
In my .cpp files, I have:
#include "/usr/local/include/opencv2/highgui/highgui.hpp"
The file libopencv_imgcodecs.so.3.2 is in my /usr/local/lib folder. I tried putting -L/usr/local/lib in the command line, but this did not help. Perhaps this file path needs to go in a specific order in the command line? The order mattered for the pkg-config --cflags --libs opencv, which had to come after the -o convert *.cpp.
Just for convenience, as the first comment said, you just need:
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/local/lib
if your CMAKE_INSTALL_PREFIX=/usr/local
Your libopencv_imgcodecs.so isn't linked correctly. You can check that by using ldd:
$ ldd ./convert
/path/to/program/convert:
...
libopencv_imgcodecs.so.3.2 => not found
libopencv_imgproc.so.3.2 => not found
libopencv_core.so.3.2 => not found
...
You can find where libopencv_imgcodecs.so is installed:
$ find / -type f -name libopencv_imgcodecs.so.3.2
/usr/local/lib/libopencv_imgcodecs.so.3.2
Save this directory to LD_LIBRARY_PATH variable.
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/local/lib

Copy and pasting .so file doesn't work with linker

I compiled and built the casablanca c++ rest library in my home directory where my absolute path to the necessary .so file was /home/dev/casablanca/Release/build.release/Binaries/libcpprest.so. What I wanted to do was to simply cp and past that .so file to /usr/lib/.. path to default lib search ../ so that I could easily link it with the following command:
g++ index.cpp -I/home/dev/casablanca/Release/include -lcpprest -std=c++11
which compiled fine, but when I ran ./a.out I got the typical runtime error:
couldn't load shared library: libcpprest.so
even after adding the default path of libcpprest.so to LD_LIBRARY_PATH.
However everything worked just fine if I linked the directory where the binary was originally created at:
// ./a.out runs just fine
g++ index.cpp -I/home/dev/casablanca/Release/include \
-L/home/dev/casablanca/Release/build.release/Binaries -lcpprest -std=c++11
I'm guessing that the reason why I can't simply move the .so object where I want to add it is somehow the compiler keeps references to it somehow. How can I install this binary in a different path?
I did compile casablanca on my linux debian ( https://git01.codeplex.com/casablanca ) with procedure https://casablanca.codeplex.com/wikipage?title=Setup%20and%20Build%20on%20Linux&referringTitle=Documentation
after compilation i get a libcpprest.so with that (objdump) :
SONAME libcpprest.so.2.2
so you might want to copy libcpprest.so.2.2 to /usr/lib/libcpprest.so.2.2
or use ldconfig tool to do so.
looking into Release/build.release/Binaries you will find :
libcpprest.so -> libcpprest.so.2.2
libcpprest.so.2.2
then libcpprest.so is just a link, real library is libcpprest.so.2.2
The section you are referring to is tuned by the rpath switch:
g++ -Wl,-rpath,/path/to/lib ...

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.

cannot open shared object file: No such file or directory

I met the share library not found on the head node of a cluster with torch. I have built the library as well as specify the correct path of the library while compiling my own program "absurdity" by g++. So it looks strange to me. Any idea? Thanks and regards!
[tim#user1 release]$ make
...
...
g++ -pipe -W -Wall -fopenmp -ggdb3 -O2 -I/home/tim/program_files/ICMCluster/ann_1.1.1/include -I/home/tim/program_files/ICMCluster/libsvm-2.89 -I/home/tim/program_files/ICMCluster/svm_light -o absurdity xxxxxx.o -L/home/tim/program_files/ICMCluster/ann_1.1.1/release/lib -L/home/tim/program_files/ICMCluster/libsvm-2.89/release/lib -L/home/tim/program_files/ICMCluster/svm_light/release/lib -lm -ljpeg -lpng -lz -lANN -lpthread -lsvm -lsvmlight
[tim#user1 release]$ ./absurdity
./absurdity: error while loading shared libraries: libsvmlight.so: cannot open shared object file: No such file or directory
[tim#user1 release]$ ls /home/tim/program_files/ICMCluster/svm_light/release/lib/libsvmlight.so -l
-rwxr-xr-x 1 tim Brown 121407 Jan 31 12:14 /home/tim/program_files/ICMCluster/svm_light/release/lib/libsvmlight.so
[tim#user1 release]$ LD_LIBRARY_PATH= /home/tim/program_files/ICMCluster/svm_light/release/lib:$LD_LIBRARY_PAT
[tim#user1 release]$ export LD_LIBRARY_PATH
[tim#user1 release]$ ./absurdity
./absurdity: error while loading shared libraries: libsvmlight.so: cannot open shared object file: No such file or directory
[tim#user1 release]$ ls /home/tim/program_files/ICMCluster/svm_light/release/lib
libsvmlight.a libsvmlight.so
Copied from my answer here: https://stackoverflow.com/a/9368199/485088
Run ldconfig as root to update the cache - if that still doesn't help, you need to add the path to the file ld.so.conf (just type it in on its own line) or better yet, add the entry to a new file (easier to delete) in directory ld.so.conf.d.
Your LD_LIBRARY_PATH doesn't include the path to libsvmlight.so.
$ export LD_LIBRARY_PATH=/home/tim/program_files/ICMCluster/svm_light/release/lib:$LD_LIBRARY_PATH
sudo ldconfig
ldconfig creates the necessary links and cache to the most recent shared libraries found in the directories specified on the command line, in the file /etc/ld.so.conf, and in the trusted directories (/lib and /usr/lib).
Generally package manager takes care of this while installing the new library, but not always (specially when you install library with cmake).
And if the output of this is empty
$ echo $LD_LIBRARY_PATH
Please set the default path
$ LD_LIBRARY_PATH=/usr/local/lib
When working on a supercomputer, I received this error when I ran:
module load python/3.4.0
screen
python
To resolve the error, I simply needed to reload the module in the screen terminal:
module load python/3.4.0
python