将库包含到我的项目文件夹中 [英] Include libraries into my project folder
问题描述
我正在使用 C++ 和 Qt Creator 进行编程.我需要在项目文件夹中包含正在使用的库,以便可以在任何设备上实现该程序.我该怎么办?
I'm programming using C++ and Qt Creator. I need to include the libraries I'm using inside my project folder so that the program can be implemented on any device. What should I do exactly?
我知道我应该修改 .pro
文件,并且我已经尝试过了:
I know I should modify the .pro
file and I already tried this:
LIBS+=-L"$$_PRO_FILE_PWD_/libs" \
-lvl \
但是它不起作用.我收到此错误:加载共享库时出错:libvl.so:无法打开共享库文件:没有这样的文件或目录
But it does not work. I get this error: error while loading shared libraries: libvl.so: cannot open shared object file: No such file or directory
有什么想法吗?谢谢
推荐答案
首先,在Linux上,qmake生成用于控制构建过程的Makefile.当您添加行
First of all, on Linux qmake generates Makefiles which are used to control your build process. When you add the line
LIBS+=-L"$$_PRO_FILE_PWD_/libs" -lvl
对您的 .pro
文件,qmake会看到生成了适当的Makefile目标,该目标告诉链接程序在 $$ _ PRO_FILE_PWD_/libs
中搜索其他库,然后链接可执行文件时,链接到库 libvl.so
.
to your .pro
file, qmake will see to it that appropriate Makefile target is generated which tells the linker to search for additional libraries in $$_PRO_FILE_PWD_/libs
and link to a library libvl.so
when linking the executable.
不过,您遇到的是运行时问题.更具体地说, ld-linux.so.2
将尝试在运行时处查找并动态加载诸如 libvl.so
之类的共享库.当加载可执行文件时,使用诸如/usr/lib
之类的内置路径和用户定义(和/或分发定义)路径来发生这种情况.我请您参考 ld-linux.so的相应手册页.2
和 ldconfig
的手册页,用于以标准方式配置用户定义的库搜索路径.
However what you're experiencing is a run-time problem. More specifically, ld-linux.so.2
will try to find and dynamically load shared libraries like libvl.so
at runtime. This happens using built-in, such as /usr/lib
and user-defined (and/or distribution-defined) paths when the executable is loaded. I refer you to the corresponding man page for ld-linux.so.2
and the man page for ldconfig
, which is used to configure user-defined library search paths in a standard way.
当 ld-linux.so.2
尝试查找库时,它将按以下顺序搜索一组定义明确的目录(所有冒号分隔):
When ld-linux.so.2
tries to find libraries, it searches a well defined set of directories (all colon-separated) in the following order:
- 如果存在的话,就是所谓的
DT_RPATH
,可以将其写入可执行库(已弃用)中. -
LD_LIBRARY_PATH
(至少对于大多数可执行文件,有关异常,请参见手册页) - 取代
DT_RPATH
(如果有)的DT_RUNPATH
,但是将查找延迟到处理LD_LIBRARY_PATH
之后.RPATH
和RUNPATH
可用于模拟Windows上的行为,在Windows上也会搜索可执行文件的路径.但是,LD_LIBRARy_PATH
更适合于此目的. 缓存文件 - 库名称,该文件由
ldconfig
使用/etc/ld.so中指定的目录生成.conf/
以及可能的其他文件,可以通过包含在ld.so.conf
中直接引用,也可以通过其他方式指定 - 受信任目录,
/lib
和/usr/lib
/etc/ld.so.cache
中存在的- if present, the so called
DT_RPATH
, which can be written into an executable library (deprecated). - the
LD_LIBRARY_PATH
(at least for most executables, see the man page for exceptions) - the
DT_RUNPATH
which supersedes theDT_RPATH
(if any) but delays lookup until afterLD_LIBRARY_PATH
has been processed. TheRPATH
andRUNPATH
can be used to simulate behavior like on Windows, where the path of the executable is also searched.LD_LIBRARy_PATH
, however, is much better suited for this purpose. - library names present in the cache file
/etc/ld.so.cache
which is generated byldconfig
using directories specified in/etc/ld.so.conf/
and possibly additional files either directly referred to via inclusion inld.so.conf
or otherwise specified - the trusted directories,
/lib
and/usr/lib
如果上述所有路径均不包含适当的共享库,则会收到错误消息,提示 ld-linux.so.2
无法加载该共享库.
If none of the paths mentioned above contain the appropriate shared object, you will get an error that it could not be loaded by ld-linux.so.2
.
您所用的解决方案很简单,并且种类繁多:
The solution in your case is simple and come in some variety:
- 在执行程序之前,使用
export LD_LIBRARY_PATH = {yourSearchPaths}
设置LD_LIBRARY_PATH
环境变量. - 将
LD_LIBRARY_PATH
添加到命令行中的可执行文件调用中,例如LD_LIBRARY_PATH = {yourSearchPaths} ./{executable}
- 提供可执行的启动Shell脚本,例如
start.sh
,它为您完成了上述操作,然后只需执行shell脚本./start.sh
- before executing the program, set the
LD_LIBRARY_PATH
environment variable usingexport LD_LIBRARY_PATH={yourSearchPaths}
. - add the
LD_LIBRARY_PATH
to the invocation of the executable on the command-line, e.g.LD_LIBRARY_PATH={yourSearchPaths} ./{executable}
- provide an executable start-up shell script, e.g.
start.sh
, which does the above for you and then simply execute the shell-script./start.sh
这篇关于将库包含到我的项目文件夹中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!