Search code examples
qtvisual-c++qmake

delayed DLL loading possible when using QMake?


In my project, I have a set of DLLs I want to load delayed, i.e. on first use instead of on process start. That means I want to use /DELAYLOAD flag of the MSVC linker (see [1] for more explanation) for certain DLLs (not Qt itself). The reason is that some users experience crashes during DLL initilization (which we can't reproduce). A former non-Qt version of the software didn't have that problem, but it used delayed loading, so that might make a difference.

Using QMake, I found no way to get delayed loading to work. Does anyone know how to pass /DELAYLOAD to the msvc linker, using qmake features on bypassing qmake?

[1] http://www.codeproject.com/KB/DLL/Delay_Loading_Dll.aspx


Solution

  • Modify .pro file:

    ## Make delayed load possible. If your project is itself a DLL which uses xxx.dll, you
    ## also need to include this line in the applications that use your DLL.
    LIBS += DelayImp.lib
    
    ## Specify that xxx.dll loading needs to be delayed
    win32:CONFIG(release, debug|release) {
        QMAKE_LFLAGS_RELEASE += /DELAYLOAD:xxx.dll
    } else:win32:CONFIG(debug, debug|release) {
        QMAKE_LFLAGS_DEBUG += /DELAYLOAD:xxx.dll
    }
    

    I use Qt5.1.1 with MSVC 2012, but according to MS this should work from VC2005 and up.