C ++:如何链接位于我的项目文件夹中的库? [英] C++: How to link a library, located in my project folder?
问题描述
我正在使用Linux和Eclipse.
我想使用一个库( libBox2D.so.2.1.0 ".我将其放在项目文件夹(也是可执行文件的工作目录)中的自制 lib
文件夹中.
我在链接器命令行参数中添加了 -lBox2D
.(这意味着我在Eclipse GUI中将"Box2D"添加为库).
I'm using Linux and Eclipse.
I want to use a library (Box2D). I have the lib-file "libBox2D.so.2.1.0
". I put it in a self-made lib
folder in my project folder (also the working directory for the executable).
I added -lBox2D
to the linker command-line arguments. (This means I added "Box2D" as library in the Eclipse GUI).
但是当我尝试运行它时,我的应用程序会自动打印出他找不到带有以下消息的" libBox2D.so.2.1.0
"库文件:
But when I try to run it, my application prints automaticly that he cannot find the "libBox2D.so.2.1.0
" library file with the following message:
error while loading shared libraries: libBox2D.so.2.1.0: cannot open shared object file: No such file or directory
我认为他正在/usr/lib/
....
I think he is searching for this library in /usr/lib/
....
因此:我如何告诉编译器/链接器/无论它需要什么",我的应用程序必须在相对路径" lib/libBox2D"下查找该库..so.2.1.0
"(相对于工作目录)?
So: How can I tell the compiler/linker/"whatever-that-needs-to-know-it" that my application has to look for the library at the relative path "lib/libBox2D.so.2.1.0
" (relative to the working dir)?
也许:问题可能出在Box2D的下载页面(上面的链接)上,他们说它是版本 2.1.2
,而该库是版本 2.1.0
(如果我寻找soso)?
Maybe: Can the problem be that on the download page of Box2D (the link here above), they say it is version 2.1.2
and that library is version 2.1.0
(if I look to the soname)?
PS:我也尝试将其放在项目文件夹的根目录中,但没有成功...
PS: I also tried to put it in the root of my project folder, but it didn't succeed...
任何帮助将不胜感激.
谢谢
Any help would be very appreciated.
Thanks
推荐答案
如果您只是在尝试一个新库,那么在开发过程中使用 LD_LIBRARY_PATH
很好.实际上,这是 LD_LIBRARY_PATH
的最合适的用法.如果您正在寻找有关如何设计和部署共享组件的建议,请继续阅读.
If you are just trying out a new library, then using LD_LIBRARY_PATH
during development is fine. In fact, this is the most appropriate use of LD_LIBRARY_PATH
. If you are looking for advice on how to design and deploy shared components, then read on.
如果您的目标是使应用程序从相对于应用程序的位置加载共享库,那么在链接可执行文件时,您可能应该考虑将运行时链接路径信息添加到可执行文件中.在加载应用程序时,加载的运行时会使用几种不同的机制来查找共享和相关对象.
If your goal is for your application to load the shared library from a location relative to the application, then you probably should look into adding runtime link path information into your executable when you link it. The runtime loaded uses a few different mechanisms for locating shared and dependent objects when the application is loaded.
- 最常见的方法是包括共享库的完整路径名.这是大多数链接程序默认情况下执行的操作.
- 另一种方法是仅包含库名称,并让加载程序搜索配置运行时路径(例如,由
ldconfig
,那些配置了LD_LIBRARY_PATH
等的对象. - 您正在寻找的方法是对相对的运行时搜索路径进行编码
根据操作系统和编译器的选择,最后一个选项的实现方式有所不同.我猜想您正在Linux机器上使用GNU工具链,因此请看 ld -rpath
选项.那里有一些很好的例子-避免LD_LIBRARY_PATH 提到 ld -rpath"$ ORIGIN/../lib"
作为选项,动态库,RPATH,... 讨论了大量与运行时路径配置有关的有趣信息.
The last option is implemented differently depending on OS and compiler choices. I'm guessing that you are using the GNU toolchain on a Linux box, so take a look at the ld -rpath
option. There are a few good examples out there - Avoiding LD_LIBRARY_PATH mentions ld -rpath '$ORIGIN/../lib'
as an option, Dynamic Libraries, RPATH, ... discusses a bunch of interesting information related to runtime path configuration.
这篇关于C ++:如何链接位于我的项目文件夹中的库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!