Congratulations to our 2022 Qt Champions!

Creating and using DLLs with the MSVC compiler in Qt Creator

  • For some time I have been using the MinGW toolchain on Windows to create a library and an application that uses the library. Let's call them MyLibrary and MyApplication. contains:
    @TEMPLATE = lib
    TARGET = MyLibrary@ contains:
    @TEMPLATE = app
    TARGET = MyApplication
    LIBS += -LMyLibraryPath -lMyLibrary@
    The MinGW toolchain produces MyLibrary.dll and libMyLibrary.a and I get no errors while building the application. Today, I tried to use the MSVC toolchain after installing the Qt SDK 1.1 with Qt 4.7.3 for MSVC 2008. When building the library I get no errors but while building the application I get: LNK1104: cannot open file 'MyLibrary.lib'. The error message is understandable since building the library did not create the lib file, only the dll file. Why isn't the lib file created? Should I use a different approach when creating and using a library?

  • Does importing and exporting the symbols as outlined in the following FAQ:

    "Creating a library ": help? See also the following link to the documentation on the same subject:

  • To be honest, I haven't tried the exporting and importing. For some reason it works perfect with MinGW without any additional export and import keywords in the source code. Maybe this is the reason it doesn't work with MSVC. If so, I think I'll stick with the MinGW toolchain for now. However, it would be nice if someone could explain to me why it works with MinGW and not with MSVC. After all, it is nice to avoid additional statements and keywords in the source code.

  • Post from IPhone. google __declspec dllexport and dllimport. There should be at least one exported function , IIRC.

  • PLease use Q_DECL_IMPORT and Q_DECL_EXPORT in Qt. Those will not break your build on non-MSVC compilers and actually work for gcc, too.

    You will need some magic to always have the right one applied. Check e.g. for an example. You then need to make sure to define (in this case) QTCREATOR_UTILS_LIB when building the library to export the symbols and leave that define out when building against the library to import them properly.

    [EDIT: fixed link, Volker]

  • The reason it works for MinGW is that gcc by default does not hide symbols in libraries. You can enable this feature even for gcc by adding:

    CONFIG += hide_symbols

    to your .pro file.

    The effect of the above is to add:

    -fvisibility=hidden -fvisibility-inlines-hidden

    as arguments to each invocation of g++.

    This will actually make your runtime startup a little quicker too since the runtime linker has less work to do (assuming you do not export absolutely everything).

  • Thanks a lot for all the good answers. I will definitely start using Q_DECL_EXPORT and Q_DECL_IMPORT in my library.

Log in to reply