简体   繁体   中英

Unresolved external using QT Creator

I started a new QT Creator project based on the SUBDIRS template and then recreated two subprojects; one DLL project and one exectuable that needs to use classes from the DLL.

I added the DLL library as an "Internal" library (and not as "external" because I figured it was in my build tree) to the executable. I have a class declared in my DLL and added the "export" specifier.

I am having trouble using it in the EXE project. It complains about unresolved external symbol. I tried adding "using MyClass;" at the top of main.cpp, but this didn't help. What do I need in place before I can use a class from my DLL project in the executable? Thanks!

Here is some code to add detail.

/////////// Main.cpp from Executable project ////////////////////////////////////////////////

#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();
}

//////////////// Class Header in 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

////////////////// Class cpp from DLL project /////////////////////////

#define ISDLL 1;

#include "myclass.h"

MyClass::MyClass()
{
}

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

//////////////////////////// Compiler output ////////////////////////////

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.

Did you put it to LIBS in pro-file? Check make files for linker flags.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM