指定Linux中C ++应用程序的静态库的位置 [英] Specify location of static libraries for C++ application in Linux

查看:85
本文介绍了指定Linux中C ++应用程序的静态库的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我希望我在正确的环境中提出这个问题...

first of all, I hope that I ask the question in the right context here...

我用C ++用Code :: Blocks构建了一个应用程序。该应用程序使用第三方提供的静态库,无法通过程序包管理将其安装在系统上。因此,在分发应用程序时,我会运送这些库。

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.

这是我的目标配置:

<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.

今天,我尝试在Debian Squeeze上运行,只是复制了一个包含可执行文件和第三方库的文件夹。我认为,只要所有内容都在一个文件夹中,可执行文件都将找到.so文件。我错了。我收到消息:

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

我在开发机器上未收到此消息,因为代码: :Blocks能够为可执行文件设置工作目录。我可以通过将.so文件的位置放在/etc/ld.so.conf.d/my_app.conf中来删除错误消息...

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...

无论如何,我可以构建可执行文件,以便它在执行目录中搜索库?还是这是Debian特有的问题?还是我可以在执行可执行文件之前为进程指定工作目录?

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...

推荐答案

首先,这些不是静态库(它们是共享的)。

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)设置 LD_LIBRARY_PATH 环境变量。

这类似于PATH,但对于共享库。

1) set the LD_LIBRARY_PATH environment variable.
This is like PATH but for shared libraries.

2)设置

这是备份到可执行文件中的路径,在其中搜索共享库

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>

可以将其设置为使其显示在当前目录中。

或设置为'$ ORIGIN'可使它显示在应用程序安装目录中。

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)您可以将它们安装到共享库的默认位置之一。

/ etc /中查看ld.so.conf ,但通常为 / usr / lib / usr / local / lib

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)您可以添加更多默认位置

修改 /etc/ld.so.conf

4) You can add more default locations
Modify /etc/ld.so.conf

这篇关于指定Linux中C ++应用程序的静态库的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆