C++ 在 Qt Creator 中使用外部 Lib/DLL?

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

Using External Lib/DLLs in Qt Creator?

c++qt

提问by George Geankins

I decided after a bunch of headaches all morning, that using Qt Creator for my first Qt project would probably be better than MSVC (had too many issues compiling).

经过一上午的一系列头痛,我决定在我的第一个 Qt 项目中使用 Qt Creator 可能比 MSVC 更好(编译问题太多)。

I am wondering though how I can add the .dlls and .libs I need for my external tools through Qt Creator. I found this post Adding external library into Qt Creator projectwhich makes sense.

我想知道如何通过 Qt Creator 添加外部工具所需的 .dll 和 .lib。我发现这篇文章将外部库添加到 Qt Creator 项目中,这是有道理的。

I need a little more info though such as...Do I link dlls or libs first, what is the syntax to add dlls to the build step in qmake (i assume its close to win32:LIBS += path/to/Psapi.lib)

我需要更多信息,例如...我先链接 dll 还是 libs,将 dll 添加到 qmake 中的构建步骤的语法是什么(我假设它接近 win32:LIBS += path/to/Psapi.库)

Thanks!

谢谢!

回答by WolfgangP

Compiling external libraries with QtCreator/gcc

使用 QtCreator/gcc 编译外部库

If you own the source code of your libraries this is the .pro file to make an external library (.dll and .a) or Framework (on Mac OS X) from them:

如果您拥有库的源代码,则这是用于从它们制作外部库(.dll 和 .a)或框架(在 Mac OS X 上)的 .pro 文件:

TEMPLATE = lib

INCLUDEPATH = <your-include-paths>
HEADERS += <your-headers>
SOURCES += <your-sources>
TARGET = MyLib  /* The name of your libary */

/* Win32: To generate a MyLib.dll and libMyLib.a (gcc) or MyLib.lib (MSVC) file */
win32 {
    CONFIG += dll
}

/* Just in case you need to generate Mac Frameworks: */
macx {
    CONFIG += shared lib_bundle
    FRAMEWORK_HEADERS.version = Versions
    FRAMEWORK_HEADERS.files += <your library headers>
    /* Example:
    FRAMEWORK_HEADERS.files += /path/to/your/lib/MyLib.h
    */
    FRAMEWORK_HEADERS.path = Headers
    QMAKE_BUNDLE_DATA = FRAMEWORK_HEADERS
    VERSION = 0.5.0   // a framework version you can define
}

Adding external libraries to your QtCreator/gcc project

将外部库添加到 QtCreator/gcc 项目

/* your project settings */

/* If you compile on windows */
win32 {
    /* If you compile with QtCreator/gcc: */
    win32-g++:LIBS += /path/to/your/libMyLib.a

    /* IF you compile with MSVC: */
    win32-msvc:LIBS += /path/to/your/libMyLib.lib
}

/* If compile on Mac and want to link against a framework */
macx {
    LIBS+= -framework MyLib
    QMAKE_FLAGS += -F/path/to/MyLib
}

Note that to use external libraries with gccyou need the libMyLib.a file that contains the linking information. The libMyLib.libare generated by MS Visual Studio and can't be processed by gcc afaik!

请注意,要使用外部库,gcc您需要包含链接信息的 libMyLib.a 文件。在libMyLib.lib被微软的Visual Studio生成,不能由GCC据我所知处理!