Qt依赖库路径配置 - 大白社区
- 发帖时间:
- 2021-10-18 17:01:33
摘要:Qt依赖库路径配置 问题 ...如果可以将依赖库的路径添加到进程的Path环境变量中,就可以直接运行程序. 这一点可以通过LIBS变量来完成. LIBS在Qt官网上并没有直接这样介绍,但在实际上它确实这样做的. .
Qt依赖库路径配置
问题
Windows下,Qt里有两个项目LockerSDKDemo及LockerSDK.
LockerSDKDemo依赖LockerSDK,而LockerSDK依赖外部库,库文件放在其根目录下的depends目录中.
如何配置可以在无需复制任何依赖库的情况下直接编译运行LockerSDKDemo?
LIBS
如果可以将依赖库的路径添加到进程的Path环境变量中,就可以直接运行程序.
这一点可以通过LIBS变量来完成.
LIBS在Qt官网上并没有直接这样介绍,但在实际上它确实这样做的.
这里比较特别的是存在多层依赖,即LockerSDKDemo依赖LockerSDK,而LockerSDK又依赖于depends.
要做的是在LockerSDKDemo同时指定LockerSDK及LockerSDK/depends的路径.即项目要配置所有的依赖路径,包括间接依赖的.
在pro文件中添加如下:
LIBS += -L$$OUT_PWD/../LockerSDK/release/ -lLockerSDK
LIBS += -L$$PWD/../LockerSDK/depends/
进程环境变量查看
ProcessExplorer功能非常强大,可以查看进程环境变量,方法如下:
从上面可以看到libs中指定的路径已经加了Path中.
PS: ProcessExplorer支持环境变量复制的,至少版本16.43支持,但15.3不支持.
References
Qt官网对LIBS的描述:
Specifies a list of libraries to be linked into the project.
If you use the Unix -l (library) and -L (library path) flags,
qmake handles the libraries correctly on Windows (that is, passes the full path of the library to the linker).
The library must exist for qmake to find the directory where a -l lib is located.
-l与-L是连接器ld的选项,下面是其GNU官网对其的说明:
-L searchdir
--library-path=searchdir
Add path searchdir to the list of paths that ld will search for archive libraries and ld control scripts.
You may use this option any number of times.
The directories are searched in the order in which they are specified on the command line.
Directories specified on the command line are searched before the default directories.
All -L options apply to all -l options,
regardless of the order in which the options appear.
-l namespec
--library=namespec
Add the archive or object file specified by namespec to the list of files to link.
This option may be used any number of times.
If namespec is of the form :filename,
ld will search the library path for a file called filename,
otherwise it will search the library path for a file called libnamespec.a.
On systems which support shared libraries,
ld may also search for files other than libnamespec.a.
Specifically, on ELF and SunOS systems, ld will search a directory for a library called libnamespec.so before searching for one called libnamespec.a.
(By convention, a ".so" extension indicates a shared library.)
Note that this behavior does not apply to :filename, which always specifies a file called filename.