【问题标题】:Using External Lib/DLLs in Qt Creator?在 Qt Creator 中使用外部库/DLL?
【发布时间】:2011-04-20 23:30:43
【问题描述】:

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

我想知道如何通过 Qt Creator 添加外部工具所需的 .dll 和 .lib。我发现这个帖子Adding external library into Qt Creator project 很有道理。

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

谢谢!

【问题讨论】:

    标签: c++ qt


    【解决方案1】:

    使用 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 无法处理!

    【讨论】:

    • 什么意思?包含文件必须像往常一样包含在您的源中。
    • 抱歉,您能否给我指出一个也适用于 Ubuntu 的模板?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-07
    • 2019-08-26
    • 1970-01-01
    • 1970-01-01
    • 2018-05-02
    相关资源
    最近更新 更多