C++ QT - 在 pro 中指定 DLL 路径。文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12326401/
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:09:40  来源:igfitidea点击:

QT - Specify DLL path in pro. file

c++qtproject-files

提问by Danran

So as the question title says, i'm specifically wondering how to include the path to a .dll file in the actually project file. I know it's the better practice to include the dll file with the project file, but i'd still just like to know if it's possible to be done?

因此,正如问题标题所说,我特别想知道如何在实际项目文件中包含 .dll 文件的路径。我知道将 dll 文件包含在项目文件中是更好的做法,但我仍然想知道是否可以完成?

Currently my .pro file consists of the following;

目前我的 .pro 文件包含以下内容;

QT       += core gui

TARGET = Test
TEMPLATE = app

win32 {
    INCLUDEPATH += "D:/Projects/Build Output/include/"

    CONFIG(debug, debug|release) {
        LIBS += "D:/Projects/Build Output/libs/debug/myLib.lib"
        LIBS += "D:/Projects/Build Output/bin/x86 debug/myLib.dll"
    } 
    else {
        LIBS += "D:/Projects/Build Output/libs/release/myLib.lib"
        LIBS += "D:/Projects/Build Output/bin/x86 release/myLib.dll"
    }
}

SOURCES += main.cpp\
    mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui

Would be cool, just to know that it can be done, thanks in advance for your help :).

会很酷,只是知道它可以完成,提前感谢您的帮助:)。

采纳答案by Penghe Geng

If you mean you want the generated exe can find its dependant dll files automatically upon you run it, then it cannot be done for implicit dll linking (i.e. linking with .lib files, as in your example). Windows has a fixed search sequenceto locate the necessary dll files. None of those sequence can be put into a QT pro file. So the following statement has no effect only makes QT know to search the dll's .lib/.a file in that path:

如果您的意思是希望生成的 exe 可以在运行时自动找到其依赖的 dll 文件,那么对于隐式 dll 链接(即与 .lib 文件链接,如您的示例中所示),则无法执行此操作。Windows 有一个固定的搜索顺序来定位必要的 dll 文件。这些序列都不能放入 QT pro 文件中。所以下面的说法没有效果 只让 QT 知道在该路径中搜索 dll 的 .lib/.a 文件:

 LIBS += "D:/Projects/Build Output/bin/x86 debug/myLib.dll"

The closest approach might be defining the dll paths as the macros in the pro file. Then use LoadLibrary to explicitly load dllsfrom those paths in your c/c++ source file. Of course only if you can settle with explicit linking instead of implicit linking,

最接近的方法可能是将 dll 路径定义为 pro 文件中的宏。然后使用 LoadLibrary从 c/c++ 源文件中的那些路径显式加载 dll。当然,只有当您可以使用显式链接而不是隐式链接来解决时,

回答by Raiden Core

You do not need to put the dll path in the .pro file (windows). All you need to do is to put all your external dll files in a directory and add this directory to the path environment variable . (same as the accepted answer). I am adding this here just to mention important fact: RESTART QT CREATOR for this to work in order to reload the new PATH environment variable.

您不需要将 dll 路径放在 .pro 文件(Windows)中。您需要做的就是将所有外部 dll 文件放在一个目录中,并将该目录添加到路径环境变量中。(与接受的答案相同)。我在这里添加这个只是为了提及一个重要的事实:重新启动 QT CREATOR 使其工作以便重新加载新的 PATH 环境变量。

回答by Luca Carlon

You just need to read the manual and do the same: http://qt-project.org/doc/qt-4.8/qmake-variable-reference.html#libs. The syntax you're using seems correct.

您只需要阅读手册并执行相同的操作:http: //qt-project.org/doc/qt-4.8/qmake-variable-reference.html#libs。您使用的语法似乎是正确的。