问题
first of all, I hope that I ask the question in the right context here...
I build an application in C++ with Code::Blocks. The application uses static libraries that are provided by a third party and cannot be installed on a system via the package management. Therefore I ship these libraries when I distribute my application.
Here is what my target configuration looks like:
<Target title="Unix_162">
<Option output="bin/my_app" prefix_auto="1" extension_auto="1" />
<Option working_dir="/home/marco/third_party_dist/lib" />
<Option object_output="obj/Unix_162" />
<Option type="1" />
<Option compiler="gcc" />
<Option use_console_runner="0" />
<Option parameters="-c" />
<Compiler>
<Add directory="/home/marco/third_party_dist/include" />
</Compiler>
<Linker>
<Add library="/home/marco/third_party_dist/lib/lib1.so" />
<Add library="/home/marco/third_party_dist/lib/lib2.so" />
<!-- some more included the same way -->
<Add directory="/home/marco/third_party_dist/lib" />
</Linker>
</Target>
I can build this target fine and run it. Everything works.
Today, I tried to run in on Debian Squeeze and just copied a folder which contained both the executable and the libraries from the third party. I thought that as long as everything is in one folder the executable will find the .so files. I was wrong. I get the message:
/home/my_app/my_app: error while loading shared libraries: lib1.so: cannot open shared object file: No such file or directory
I don't get this message on my developement machine because Code::Blocks is able to set a working directory for the executable. I could remove the error message by putting the location of the .so files inside /etc/ld.so.conf.d/my_app.conf...
Is there anyway I can build the executable so it searches the libs in the execution directory? Or this is a problem specific for Debian? Or can I specify the working directory for the process before I execute the executable?
I want to avoid changing the systems configuration / environment before you can start the application...
回答1:
First point these are not static libraries (they are shared).
So the problem is locating the libraries at runtime.
There are a couple of ways of doing this:
1) set the LD_LIBRARY_PATH
environment variable.
This is like PATH but for shared libraries.
2) set the rpath in the executable.
This is a path backed into the executable where is searches for shared libs
-Wl,-rpath,<LIB_INSTALL_PATH>
This can be set to .
which will make it look in the current directory.
or you can set to '$ORIGIN'
which will make it look in the directory the application is installed in.
3) You can install them into one of the default locations for shared libraries.
Look inside /etc/ld.so.conf
but usually /usr/lib
and /usr/local/lib
4) You can add more default locations
Modify /etc/ld.so.conf
回答2:
Yes there is, you have to pass option -rpath <path>
to your linker where <path>
is the path of your library (similar to option -L
).
Also, you are probably talking about shared libraries, not static ones.
回答3:
I thought that as long as everything is in one folder the executable will find the .so files. I was wrong.
An extra step is required to make Linux dynamic linker look for shared libraries in the same directory as the executable. Link the executable with -Wl,-rpath,'$ORIGIN'
option (in the makefile $
needs to be quoted like -Wl,-rpath,'$$ORIGIN'
). See $ORIGIN and rpath note for more details.
来源:https://stackoverflow.com/questions/11780089/specify-location-of-static-libraries-for-c-application-in-linux