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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/718447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:55:47  来源:igfitidea点击:

Adding external library into Qt Creator project

c++winapiqtqt-creator

提问by Donotalo

How can I add external library into a project built by Qt Creator RC1 (version 0.9.2)? For example, the win32 function EnumProcesses()requires Psapi.libto be added in the project to build.

如何将外部库添加到由 Qt Creator RC1(版本 0.9.2)构建的项目中?比如EnumProcesses()需要Psapi.lib在工程中加入win32函数才能构建。

回答by

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. The idea is that you have to separate the directory from the library name (without the extension and without any 'lib' prefix). Of course, if you are including a Windows specific lib, this really doesn't matter.

这样它就可以在 Qt 支持的所有平台上工作。这个想法是你必须将目录与库名分开(没有扩展名,也没有任何“lib”前缀)。当然,如果您包含 Windows 特定的库,这真的无关紧要。

In case you want to store your lib files in the project directory, you can reference them with the $$_PRO_FILE_PWD_variable, e.g.:

如果您想将 lib 文件存储在项目目录中,您可以使用$$_PRO_FILE_PWD_变量引用它们,例如:

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

回答by dirkgently

Are you using qmakeprojects? If so, you can add an external library using the LIBSvariable. E.g:

你在使用qmake项目吗?如果是这样,您可以使用该LIBS变量添加外部库。例如:

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

回答by martin

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". I recommend placing libraries in non white-space locations ;-)

将不起作用,因为您在程序文件中使用空格。在这种情况下,您必须添加引号,因此结果将如下所示:LIBS += "C:\Program Files\OpenCV\lib"。我建议将库放在非空白位置;-)

回答by martin

The error you mean is due to missing additional include path. Try adding it with: INCLUDEPATH += C:\path\to\include\files\ Hope it works. Regards.

您的意思是由于缺少其他包含路径而导致的错误。尝试添加它: INCLUDEPATH += C:\path\to\include\files\ 希望它有效。问候。

回答by QT-ITK-VTK-Help

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

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

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 -lvtlvtlgtcflvtcflvtlfkexp -lvtkfreetype -lvtkHybrid -lvtkVolumeRendering -lQVTKWidgetPlugin -lvtkGenericFiltering

回答by adlag

If you want to deploy your application on machines of customers, rather than using your application only yourself, we find that the LIBS+= -Lxxx -lyyymethod can lead to confusion if not problems.

如果您想将您的应用程序部署在客户的机器上,而不是只使用您自己的应用程序,我们发现该LIBS+= -Lxxx -lyyy方法即使没有问题也会导致混淆。

We develop applications for Linux, Mac and Windows using Qt. 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. For reasons of platform compatibility the USB stick must then be formatted as FAT32, which does not support (Linux) symlinks.

我们使用 Qt 为 Linux、Mac 和 Windows 开发应用程序。我们提供完整的独立应用程序。所以所有非系统库都应该包含在部署包中。我们希望我们的客户能够从同一个 USB 记忆棒为所有操作系统运行应用程序。出于平台兼容性的原因,USB 记忆棒必须格式化为 FAT32,它不支持 (Linux) 符号链接。

We found the LIBS+= -Lxxx -lyyyidiom 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. 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.

  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 nameof the library whether we link a static or a dynamic library.

  1. 我们并不完全知道链接器找到的(静态或动态)库的文件路径是什么。这很不方便。我们的 Mac 链接器经常发现与我们认为应该使用的库不同的库。这种情况在 OpenSSL 库中发生过多次,Mac 链接器发现并使用了它自己的(较旧的、不兼容的)OpenSSL 版本,而不是我们请求的版本。

  2. 我们不能承受链接器使用指向库的符号链接,因为这会破坏部署包。

  3. 我们想从库中看出我们链接的是静态库还是动态库。

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. And, for instance for Linux 64bit, then:

首先,我们找出我们使用的操作系统并将其放入 CONFIG 变量中。并且,例如对于 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.

所有依赖项都可以复制到部署包中,因为我们知道它们的文件路径。

回答by zar

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

基本上,如果您不提供实际的库名称,它会添加搜索依赖库的位置的路径。语法上的差异很小,但这对于仅提供 PATH 查找依赖库的位置非常有用。有时提供每个路径单独的库是一件很痛苦的事情,你知道它们都在某个文件夹中,Qt Creator 会选择它们。

回答by Dnyaneshwar

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")