PDA

View Full Version : Can't run executable file directly: Cannot open shared object file



rockballad
1st January 2010, 17:18
Hi all

I built a library. Then another application to load that lib. I can build and run it using Qt Creator. But I can't execute the application in commandline (terminal). The error is: "Error while loading shared libraries: libtest.so.1: cannot open shared object file: no such file or directory.", despite that fact that I moved the library file to the directory of executable file (application).

Please tell me how to get out of this "stupid" problem.

Thanks a lot!

Happy New Year of 2010, everybody!

Cheers,

wysota
1st January 2010, 18:09
despite that fact that I moved the library file to the directory of executable file (application).
This only works in Windows. On Unix the dynamic loader needs to be informed where the libraries are. If you don't want to place a library in one of the standard locations (like /usr/lib) you need to point the linker to the proper place. On Linux you would do it by setting the LD_LIBRARY_PATH environment variable to point to the directory containing your libraries. GCC can also hardcode a library search path by using the so called "rpath" linker directive.

rockballad
2nd January 2010, 01:11
Thanks for your reply. That was I thought. Of course I did search a lot before posting the question here. But I also found that we should not use LD_LIBRARY_PATH (http://linuxmafia.com/faq/Admin/ld-lib-path.html). Now I'm trying to use LD_LIBRARY_PATH, as follows

1/ Or I keep the old setting: LIB += libmylib.so
2/ Or I specify LIBS += -L../mylibdir -lmylib -R../mylibdir

with the mylibdir directory contains the libmylib.so file.
I can build and run on Qt Creator. Outside, on Terminal, I "export LD_LIBRARY_PATH" before running, it's okay. At least it works.

May I ask if we can specify the LD_LIBRARY_PATH on .pro file and after building successfully, we can run the executable file directly (No export action)?

Thank you!

wysota
2nd January 2010, 08:46
May I ask if we can specify the LD_LIBRARY_PATH on .pro file and after building successfully, we can run the executable file directly (No export action)?

No, it's strictly a runtime setting. But you can use rpath, as already suggested.

http://en.wikipedia.org/wiki/Rpath_%28linking%29

rockballad
2nd January 2010, 10:26
No, it's strictly a runtime setting. But you can use rpath, as already suggested.

http://en.wikipedia.org/wiki/Rpath_%28linking%29

Thank you, wysota!

cutie.monkey
15th June 2010, 07:42
You can also create a shell script like this:


#!bin/bash
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:"path_of_your_libs"
export LD_LIBRARY_PATH
exec "./app_name"


Then make the script executable:


$chmod +x script_name


Hope this help! Cheers!