简体   繁体   English

将外部库添加到 Qt Creator 项目中

[英]Adding external library into Qt Creator project

How can I add external library into a project built by Qt Creator RC1 (version 0.9.2)?如何将外部库添加到由 Qt Creator RC1(版本 0.9.2)构建的项目中? For example, the win32 function EnumProcesses() requires Psapi.lib to be added in the project to build.比如win32函数EnumProcesses()需要在项目中添加Psapi.lib才能构建。

The proper way to do this is like this:正确的做法是这样的:

LIBS += -L/path/to -lpsapi

This way it will work on all platforms supported by Qt.这样它就可以在 Qt 支持的所有平台上工作。 The idea is that you have to separate the directory from the library name (without the extension and without any 'lib' prefix).这个想法是你必须将目录与库名分开(没有扩展名,也没有任何“lib”前缀)。 Of course, if you are including a Windows specific lib, this really doesn't matter.当然,如果您包含 Windows 特定的库,这真的无关紧要。

In case you want to store your lib files in the project directory, you can reference them with the $$_PRO_FILE_PWD_ variable, eg:如果要将 lib 文件存储在项目目录中,可以使用$$_PRO_FILE_PWD_变量引用它们,例如:

LIBS += -L"$$_PRO_FILE_PWD_/3rdparty/libs/" -lpsapi

Are you using qmake projects?你在使用qmake项目吗? If so, you can add an external library using theLIBS variable.如果是这样,您可以使用LIBS变量添加外部库。 Eg:例如:

win32:LIBS += path/to/Psapi.lib

LIBS += C:\\Program Files\\OpenCV\\lib LIBS += C:\\Program Files\\OpenCV\\lib

won't work because you're using white-spaces in Program Files.将不起作用,因为您在程序文件中使用空格。 In this case you have to add quotes, so the result will look like this: LIBS += "C:\\Program Files\\OpenCV\\lib" .在这种情况下,您必须添加引号,因此结果将如下所示: LIBS += "C:\\Program Files\\OpenCV\\lib" I recommend placing libraries in non white-space locations ;-)我建议将库放在非空白位置;-)

The error you mean is due to missing additional include path.您的意思是由于缺少其他包含路径而导致的错误。 Try adding it with: INCLUDEPATH += C:\\path\\to\\include\\files\\ Hope it works.尝试添加它: INCLUDEPATH += C:\\path\\to\\include\\files\\ 希望它有效。 Regards.问候。

And to add multiple library files you can write as below:并添加多个库文件,您可以编写如下:

INCLUDEPATH *= E:/DebugLibrary/VTK E:/DebugLibrary/VTK/Common E:/DebugLibrary/VTK/Filtering E:/DebugLibrary/VTK/GenericFiltering E:/DebugLibrary/VTK/Graphics E:/DebugLibrary/VTK/GUISupport/Qt E:/DebugLibrary/VTK/Hybrid E:/DebugLibrary/VTK/Imaging E:/DebugLibrary/VTK/IO E:/DebugLibrary/VTK/Parallel E:/DebugLibrary/VTK/Rendering E:/DebugLibrary/VTK/Utilities E:/DebugLibrary/VTK/VolumeRendering E:/DebugLibrary/VTK/Widgets E:/DebugLibrary/VTK/Wrapping INCLUDEPATH *= E:/DebugLibrary/VTK E:/DebugLibrary/VTK/Common E:/DebugLibrary/VTK/Filtering E:/DebugLibrary/VTK/GenericFiltering E:/DebugLibrary/VTK/Graphics E:/DebugLibrary/VTK/GUISupport/ Qt E:/DebugLibrary/VTK/Hybrid E:/DebugLibrary/VTK/Imaging E:/DebugLibrary/VTK/IO E:/DebugLibrary/VTK/Parallel E:/DebugLibrary/VTK/Rendering E:/DebugLibrary/VTK/Utilities E :/DebugLibrary/VTK/VolumeRendering E:/DebugLibrary/VTK/Widgets E:/DebugLibrary/VTK/Wrapping

LIBS *= -LE:/DebugLibrary/VTKBin/bin/release -lvtkCommon -lvtksys -lQVTK -lvtkWidgets -lvtkRendering -lvtkGraphics -lvtkImaging -lvtkIO -lvtkFiltering -lvtkDICOMParser -lvtkpng -lvtktiff -lvtkzlib -lvtkjpeg -lvtkexpat -lvtkNetCDF -lvtkexoIIc -lvtkftgl -lvtkfreetype -lvtkHybrid -lvtkVolumeRendering -lQVTKWidgetPlugin -lvtkGenericFiltering LIBS *= -LE:/DebugLibrary/VTKBin/bin/release -lvtkCommon -lvtksys -lQVTK -lvtkWidgets -lvtkRendering -lvtkGraphics -lvtkImaging -lvtkIO -lvtkFiltering -lvtkDICOMParser -lvtkpng -lvtktiff -lvtlvtlgtcflvtcflvtlfkexp -lvtkfreetype -lvtkHybrid -lvtkVolumeRendering -lQVTKWidgetPlugin -lvtkGenericFiltering

If you want to deploy your application on machines of customers, rather than using your application only yourself, we find that the LIBS+= -Lxxx -lyyy method can lead to confusion if not problems.如果您想在客户的机器上部署您的应用程序,而不是只使用您自己的应用程序,我们发现LIBS+= -Lxxx -lyyy方法即使没有问题也会导致混淆。

We develop applications for Linux, Mac and Windows using Qt.我们使用 Qt 为 Linux、Mac 和 Windows 开发应用程序。 We ship complete, stand-alone applications.我们提供完整的独立应用程序。 So all non-system libraries should be included in the deployment package.所以所有非系统库都应该包含在部署包中。 We want our customers to be able to run the application from the same USB stick for all OSs.我们希望我们的客户能够从同一个 USB 记忆棒为所有操作系统运行应用程序。 For reasons of platform compatibility the USB stick must then be formatted as FAT32, which does not support (Linux) symlinks.出于平台兼容性的原因,USB 记忆棒必须格式化为 FAT32,它不支持 (Linux) 符号链接。

We found the LIBS+= -Lxxx -lyyy idiom too much of a black box:我们发现LIBS+= -Lxxx -lyyy习语太多了:

  1. We do not exactly know what the filepath is of the (static or dynamic) library that has been found by the linker.我们并不完全知道链接器找到的(静态或动态)库的文件路径是什么。 This is inconvenient.这很不方便。 Our Mac linker regularly found libs different from the ones we thought that should be used.我们的 Mac 链接器经常发现与我们认为应该使用的库不同的库。 This happened several times with OpenSSL libraries where the Mac linker found and used its own - older, incompatible - OpenSSL version rather than our requested version.这种情况在 OpenSSL 库中发生过多次,Mac 链接器发现并使用了它自己的(较旧的、不兼容的)OpenSSL 版本,而不是我们请求的版本。

  2. We cannot afford that the linker uses symlinks to libraries as this would break the deployment package.我们不能承受链接器使用指向库的符号链接,因为这会破坏部署包。

  3. We want to see from the name of the library whether we link a static or a dynamic library.我们想从库中看出我们链接的是静态库还是动态库。

So for our particular case we use only absolute filepaths and check whether they exist.所以对于我们的特殊情况,我们只使用绝对文件路径并检查它们是否存在。 We remove all symlinks.我们删除所有符号链接。

First we find out what operating system we are using and put this in the CONFIG variable.首先,我们找出我们使用的操作系统并将其放入 CONFIG 变量中。 And, for instance for Linux 64bit, then:并且,例如对于 Linux 64 位,则:

linux64 {
    LIBSSL= $$OPENSSLPATH/linux64/lib/libssl.a
    !exists($$LIBSSL): error ("Not existing $$LIBSSL")
    LIBS+= $$LIBSSL
    LIBCRYPTO= $$OPENSSLPATH/linux64/lib/libcrypto.a
    !exists($$LIBCRYPTO): error ("Not existing $$LIBCRYPTO")
    LIBS+= $$LIBCRYPTO
}

All the dependencies can be copied into deployment package as we know their filepaths.所有依赖项都可以复制到部署包中,因为我们知道它们的文件路径。

I would like to add for the sake of completeness that you can also add just the LIBRARY PATH where it will look for a dependent library (which may not be directly referenced in your code but a library you use may need it).为了完整起见,我想补充一点,您还可以仅添加 LIBRARY PATH,它会在其中查找依赖库(它可能不会在您的代码中直接引用,但您使用的库可能需要它)。

For comparison, this would correspond to what LIBPATH environment does but its kind of obscure in Qt Creator and not well documented.为了进行比较,这与 LIBPATH 环境所做的相对应,但它在 Qt Creator 中有点模糊并且没有很好的文档记录。

The way i came around this is following:我解决这个问题的方式如下:

LIBS += -L"$$_PRO_FILE_PWD_/Path_to_Psapi_lib/"

Essentially if you don't provide the actual library name, it adds the path to where it will search dependent libraries.基本上,如果您不提供实际的库名称,它会添加搜索依赖库的位置的路径。 The difference in syntax is small but this is very useful to supply just the PATH where to look for dependent libraries.语法上的差异很小,但这对于仅提供 PATH 查找依赖库的位置非常有用。 It sometime is just a pain to supply each path individual library where you know they are all in certain folder and Qt Creator will pick them up.有时提供每个路径单独的库是一件很痛苦的事情,你知道它们都在某个文件夹中,Qt Creator 会选择它们。

in .pro : LIBS += Ole32.lib OleAut32.lib Psapi.lib advapi32.lib在 .pro 中: LIBS += Ole32.lib OleAut32.lib Psapi.lib advapi32.lib

in .h/.cpp: #pragma comment(lib,"user32.lib")在 .h/.cpp 中: #pragma comment(lib,"user32.lib")

#pragma comment(lib,"psapi.lib")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM