1

我基于 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();
}

///////////////////////////////////////////////////////////////////////////////////////////////// //

#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.
4

1 回答 1

0

你把它放在档案里的LIBS了吗?检查链接器标志的 make 文件。

于 2013-09-17T22:29:37.253 回答