Important: Please read the Qt Code of Conduct -

Linking QT Application to Visual Studio Static 32 bit library fails

  • Hello All,

    I am trying to get up to speed with adding a new GUI front end to a previously console based application. The existing application was developed in Visual Studio 2012 Professional. The existing console application (executable) comprises a main() and about 20 or so static 32 bit libraries.

    I used the QT Creator (qt-windows-opensource-5.1.1-msvc2012_opengl-x86_64-offline & qt-vs-addin-1.2.2-opensource) to create a dummy application (using the built in QT Gui Application wizard) and then I choose the add library context popup to add one of these static libraries to the linker.

    Next I added a single line of code to use the generated main.cpp to bring in a reference from the static library and after qmaking and building the project, it complained as follows:

    @main.obj:-1: error: LNK2019: unresolved external symbol "public: __cdecl LoadsetList::LoadsetList(void)" (??0LoadsetList@@QEAA@XZ) referenced in function main
    main.obj:-1: error: LNK2019: unresolved external symbol "public: virtual __cdecl LoadsetList::~LoadsetList(void)" (??1LoadsetList@@UEAA@XZ) referenced in function main
    debug\vcbuild.exe:-1: error: LNK1120: 2 unresolved externals@

    Can anyone shed any light on what might be going wrong.

    Here is the project file (note that the include paths are setup to point to my existing source code for the static libraries and QT seems to recognize the code by auto-completion.

    Project created by QtCreator 2013-10-29T17:27:49


    QT += core gui

    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

    TARGET = vcbuild
    TEMPLATE = app

    SOURCES += main.cpp

    HEADERS += mainwindow.h

    INCLUDEPATH += /main/ltib/cpas/include

    win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../ltib/cpas/win32/loadset/release/ -lloadset
    else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../ltib/cpas/win32/loadset/debug/ -lloadset

    INCLUDEPATH += $$PWD/../../ltib/cpas/win32/loadset/Debug
    DEPENDPATH += $$PWD/../../ltib/cpas/win32/loadset/Debug

    win32:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/../../ltib/cpas/win32/loadset/release/loadset.lib
    else:win32:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/../../ltib/cpas/win32/loadset/debug/loadset.lib

    Here is the modified main.cpp: Note I simply put a single line referring to the LoadsetList object which resides in my static library

    @#include <QApplication>
    #include "mainwindow.h"
    #include "loadset/LoadsetList.h"

    int main(int argc, char *argv[])
    QApplication a(argc, argv);
    MainWindow w;;
    LoadsetList loadsetList;
    return a.exec();

  • Lifetime Qt Champion


    I think the first problem you have is that you try to link 32bit libs to a 64bit Qt. You'll need to either use a 32bit Qt or a 64bit version of your libs

  • Thanks, it looks like that seems to be the problem. I'm now facing having to port the existing code to x64. I noticed that in order to get the library to link correctly, I needed to create it as "Multi-threaded Debug DLL (/MDd)" - not doing so caused the following error:

    @error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MDd_DynamicDebug' doesn't match value 'MTd_StaticDebug' in moc_mainwindow.obj
    It looks like the autogenerated moc_mainwindow uses the /MDd build option and I need to match this. I'm not sure if there is some way of linking to my Static debug libraries - as my existing code is made up of libs and not DLLs.

  • Lifetime Qt Champion

    Not necessarily, you can also use a 32bit Qt. By default Qt is build against the dynamic runtime.

    You can also make your static library link against the dynamic runtime

  • As posted above, I already tried to make the default 64 bit QT link with a test 64 bit static library which I set to use static debug output) - However, that failed with the LNK2038 indicated.

    Is there some QT (.pro) or Visual C++ project build setting I am missing in order to get the default 64 bit QT (which is built against the Dynamic run-time) to link with a static library like I have above?

    My only work around was to change the flags to match the QT's dynamic run-time). I would prefer not to recompile QT to static mode as I think that there are some licensing issues associated with that approach.

  • Lifetime Qt Champion

    AFAIK no, you have to use the same runtime linking. You could try to build Qt dynamically against the static runtime though (I don't know if it'll work) for that you need to edit the mkspec for your version of visual studio and replace all MDD occurrences with MTD

Log in to reply