【问题标题】:Unresolved external using QT Creator使用 QT Creator 未解决的外部问题
【发布时间】:2013-09-17 22:19:42
【问题描述】:

我基于 SUBDIRS 模板新建了一个 QT Creator 项目,然后重新创建了两个子项目;一个 DLL 项目和一个需要使用 DLL 中的类的可执行文件。

我将 DLL 库作为“内部”库(而不是“外部”库,因为我认为它在我的构建树中)添加到可执行文件中。我在我的 DLL 中声明了一个类并添加了“导出”说明符。

我在 EXE 项目中使用它时遇到问题。它抱怨未解决的外部符号。我尝试添加“使用 MyClass;”在 main.cpp 的顶部,但这没有帮助。在我可以在可执行文件中使用我的 DLL 项目中的类之前,我需要什么?谢谢!

这是一些添加细节的代码。

////////// 来自可执行项目的 Main.cpp ///////////////////////// /////////////////

#define USEDLL 1;

#include <QCoreApplication>
#include "../Library/myclass.h"
#include <QDebug>
#include <iostream>

//__declspec(dllimport) MyClass;


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    __declspec(dllimport) MyClass g;
    QString test;

    test = MyClass::TheString();

    return a.exec();
}

////////////// DLL 中的类头 /////////////////////// /////

#ifndef MYCLASS_H
#define MYCLASS_H

#include <QString>

#ifdef ISDLL
    #define DLL __declspec(dllexport)
#endif

#ifdef USEDLL
    #define DLL __declspec(dllimport)
#endif

class DLL MyClass
{

public:
    MyClass();
    static QString TheString();
};

#endif // MYCLASS_H

/////////////// 来自 DLL 项目的类 cpp ///////////////////// /

#define ISDLL 1;

#include "myclass.h"

MyClass::MyClass()
{
}

QString MyClass::TheString()
{
    return "test";
}

////////////////////////编译器输出////////////// ///////////

11:09:59: Running steps for project Top...
11:09:59: Configuration unchanged, skipping qmake step.
11:09:59: Starting: "C:\Qt\Tools\QtCreator\bin\jom.exe" 
    cd Library\ && ( if not exist Makefile C:\Qt\5.1.1\msvc2010\bin\qmake.exe D:\Projects\Top\Library\Library.pro -spec win32-msvc2010 CONFIG+=debug CONFIG+=declarative_debug CONFIG+=qml_debug -o Makefile ) && C:\Qt\Tools\QtCreator\bin\jom.exe -f Makefile
    C:\Qt\Tools\QtCreator\bin\jom.exe -f Makefile.Debug
    cd App\ && ( if not exist Makefile C:\Qt\5.1.1\msvc2010\bin\qmake.exe D:\Projects\Top\App\App.pro -spec win32-msvc2010 CONFIG+=debug CONFIG+=declarative_debug CONFIG+=qml_debug -o Makefile ) && C:\Qt\Tools\QtCreator\bin\jom.exe -f Makefile
    C:\Qt\Tools\QtCreator\bin\jom.exe -f Makefile.Debug
    cl -c -nologo -Zm200 -Zc:wchar_t -Zi -MDd -GR -W3 -w34100 -w34189 -EHsc -DUNICODE -DWIN32 -DQT_QML_DEBUG -DQT_DECLARATIVE_DEBUG -DQT_CORE_LIB -I"C:\Qt\5.1.1\msvc2010\include" -I"C:\Qt\5.1.1\msvc2010\include\QtCore" -I"debug" -I"." -I"C:\Qt\5.1.1\msvc2010\mkspecs\win32-msvc2010" -Fodebug\ @C:\Users\philip\AppData\Local\Temp\main.obj.4512.0.jom
main.cpp
    echo 1 /* CREATEPROCESS_MANIFEST_RESOURCE_ID */ 24 /* RT_MANIFEST */ "debug\\App.exe.embed.manifest">debug\App.exe_manifest.rc
    if not exist debug\App.exe if exist debug\App.exe.embed.manifest del debug\App.exe.embed.manifest
    if exist debug\App.exe.embed.manifest copy /Y debug\App.exe.embed.manifest debug\App.exe_manifest.bak
    link /NOLOGO /DYNAMICBASE /NXCOMPAT /DEBUG /SUBSYSTEM:CONSOLE "/MANIFESTDEPENDENCY:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' publicKeyToken='6595b64144ccf1df' language='*' processorArchitecture='*'" /MANIFEST /MANIFESTFILE:debug\App.exe.embed.manifest /OUT:debug\App.exe @C:\Users\philip\AppData\Local\Temp\App.exe.4512.687.jom
main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: static class QString __cdecl MyClass::TheString(void)" (__imp_?TheString@MyClass@@SA?AVQString@@XZ) referenced in function _main
debug\App.exe : fatal error LNK1120: 1 unresolved externals
jom: D:\Projects\build-Top-Desktop_Qt_5_1_1_MSVC2010_32bit-Debug\App\Makefile.Debug [debug\App.exe] Error 1120
jom: D:\Projects\build-Top-Desktop_Qt_5_1_1_MSVC2010_32bit-Debug\App\Makefile [debug] Error 2
jom: D:\Projects\build-Top-Desktop_Qt_5_1_1_MSVC2010_32bit-Debug\Makefile [sub-App-make_first-ordered] Error 2
11:10:00: The process "C:\Qt\Tools\QtCreator\bin\jom.exe" exited with code 2.
Error while building/deploying project Top (kit: Desktop Qt 5.1.1 MSVC2010 32bit)
When executing step 'Make'
11:10:00: Elapsed time: 00:01.

【问题讨论】:

    标签: c++ linker qt-creator external


    【解决方案1】:

    你有没有把它放到 LIBS 的配置文件中?检查 make 文件中的链接器标志。

    【讨论】:

    • 我想是的。 QT Creator GUI 将以下内容插入到 EXE 的 pro 文件中: win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../GCDCore/release/ -lGCDCore else:win32:CONFIG(调试,调试|发布):LIBS += -L$$OUT_PWD/../GCDCore/debug/ -lGCDCore else:unix: LIBS += -L$$OUT_PWD/../GCDCore/ -lGCDCore INCLUDEPATH += $$ PWD/../GCDCore DEPENDPATH += $$PWD/../GCDCore
    • 您是否将 __declspec(dllexport) 添加到所有导出的类中?
    • 是的。我已经尝试了您建议的说明符(类 __declspec(dllexport) MyProject)以及使用 _global.h 文件中定义的宏。我将它包含在类 .h 文件中。没有运气
    • 我是否需要在 main.cpp 的顶部显示我正在使用外部类/类型?像“使用”或“外部”语句?
    • 您需要包括:#include "yourclass.h"。但是如果缺少包含的错误将是“YourClass 未在此范围内声明”,因此问题出在链接中。