c++ - 在 Qt Creator 中使用外部库/DLL?

标签 c++ qt

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

我想知道如何通过 Qt Creator 添加外部工具所需的 .dll 和 .lib。我找到这篇文章 Adding external library into Qt Creator project这是有道理的。

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

谢谢!

最佳答案

使用QtCreator/gcc编译外部库

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

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
}

将外部库添加到您的 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
}

请注意,要将外部库与 gcc 一起使用,您需要包含链接信息的 libMyLib.a 文件。 libMyLib.lib 由 MS Visual Studio 生成,gcc afaik 无法处理!

关于c++ - 在 Qt Creator 中使用外部库/DLL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3858399/

相关文章:

c++ - 网格中的相邻产品

c++ - C++中的凯撒密码

python - 无法在场景中正确定位 QGraphicsRectItem

mysql - Qt 库 'mysql' 未定义

c++ - 找不到 QByteArray 成员

c++ - 定义 volatile 类对象

c++ - 计算旋转矩形的顶点

c++ - 使用 pthreads 读取输入

qt - 需要 QGraphicsScene 信号或事件以进行 _after_ 更改

qt - 使用 QT Designer 在 GUI 中插入图像