PDA

View Full Version : QMake - Linking to Static Libraries (win32-msvc2008) - Unresolved Externals



snapcall
14th October 2009, 05:31
As a preface: I have compiled Qt statically with the win32-msvc2008 compiler using both the "-O1 -MD" and "-O1 -MT" compiler flags.

In Qt Creator, I wanted to create a simple Win32 DLL that statically linked to Qt:



// main.cpp
#include <QtCore/QString>
#include <QtCore/QtDebug>
#include "windows.h"

bool WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
QString temp("This is a QString");
qDebug() << temp;
return true;
}


I then used the following .pro file:



// test.pro
QT -= gui
TARGET = test
TEMPLATE = lib
CONFIG += dll
DEFINES += TEST_LIBRARY
SOURCES += main.cpp
HEADERS +=


However, the Qt libraries are not correctly linked (unresolved externals). See the following build output:


Starting: G:/dev/libraries/vendor/nokia/Qt/4.5.3/msvc2008-static/bin/qmake.exe G:/dev/sandbox/test/test.pro -spec win32-msvc2008 -r
Exited with code 0.
Starting: C:/Program Files (x86)/Microsoft Visual Studio 9.0/VC/BIN/nmake.exe
"C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\BIN\nmake.exe" -f Makefile.Release
cl -c -nologo -Zm200 -Zc:wchar_t- -O1 -MD -GR -EHsc -W3 -w34100 -w34189 -DUNICODE -DWIN32 -DQT_LARGEFILE_SUPPORT \
-DTEST_LIBRARY -DQT_DLL -DQT_NO_DEBUG -DQT_CORE_LIB -DQT_THREAD_SUPPORT \
-I"..\..\libraries\vendor\nokia\Qt\4.5.3\msvc2008-static\include\QtCore" \
-I"..\..\libraries\vendor\nokia\Qt\4.5.3\msvc2008-static\include" \
-I"..\..\libraries\vendor\nokia\Qt\4.5.3\msvc2008-static\include\ActiveQt" \
-I"release" -I"..\..\libraries\vendor\nokia\Qt\4.5.3\msvc2008-static\mkspecs\win32-msvc2008" \
-Forelease\ @C:\Users\Steven\AppData\Local\Temp\nm111.tmp
main.cpp

link /LIBPATH:"g:\dev\libraries\vendor\nokia\Qt\4.5.3\msvc2008-static\lib" /NOLOGO /INCREMENTAL:NO /DLL /OUT:release\test.dll @C:\Users\Steven\AppData\Local\Temp\nm44D.tmp
main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall QString::~QString(void)" (__imp_??1QString@@QAE@XZ) referenced in function _DllMain@12
main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall QDebug::~QDebug(void)" (__imp_??1QDebug@@QAE@XZ) referenced in function _DllMain@12
main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: class QDebug & __thiscall QDebug::operator<<(class QString const &)" (__imp_??6QDebug@@QAEAAV0@ABVQString@@@Z) referenced in function _DllMain@12
main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) class QDebug __cdecl qDebug(void)" (__imp_?qDebug@@YA?AVQDebug@@XZ) referenced in function _DllMain@12
main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) private: static struct QString::Data * __cdecl QString::fromAscii_helper(char const *,int)" (__imp_?fromAscii_helper@QString@@CAPAUData@1@PBDH @Z) referenced in function _DllMain@12
release\test.dll : fatal error LNK1120: 5 unresolved externals


I have checked the .tmp files and QtCore.lib is indeed included in the nmake command--this baffles me. Compiling the test project in Visual Studio 2008 correctly links to the Qt static libraries.

Does anyone know what might be causing the problem and how I can go about fixing it?

snapcall
31st October 2009, 00:45
bump

Does anyone have any ideas?

snapcall
31st October 2009, 04:41
After doing extensive tests by compiling and linking the app manually on the command line and adding/removing arguments, I believe I found the problem. The unresolved externals link errors occur when QT_DLL is defined.

The solution is to define QT_NODLL in your .pro (DEFINES += QT_NODLL), as qmake automatically inserts -DQT_DLL when QT_NODLL is not defined (see mkspecs/features/qt.prf).