Friday, April 26, 2019

Linux and C++: cannot open shared object file

When we build a program with the C++ compiler we may specify the location of the depending shared libraries with command line parameters. For example when using g++, we use command line parameter -L to specify the directory where the shared library locates:

$ g++ -shared -L/path/to/shared/lib/directory -lmylib ...

We may also use the environmental variable LD_LIBRARY_PATH to add the directory of the shared libraries to the compiling environment:

$ export  LD_LIBRARY_PATH=/path/to/shared/lib/directory:$LD_LIBRARY_PATH

However, the runtime environment of the program may not have the directory in the system path and LD_LIBRARY_PATH may not have been set. It is likely that this kind of errors are spit out when running the program:

cannot open shared object file: No such file or directory.

we can use the command ldd (a tool to print shared lib dependencies) to check the dependencies of the program. Those shared libraries are listed as "not found".

To resolve this issue, either put those shared libraries in the system path for shared libraries. Or we can set the environmental variable LD_LIBRARY_PATH before running the program:

$ export  LD_LIBRARY_PATH=/path/to/shared/lib/directory:$LD_LIBRARY_PATH
$ ./myprogram

No comments:

Get This <