Qt World Summit: Register Today!

Add DLL paths at runtime

  • Hello,

    In a CMake project, I have 2 different output folders for library files and executable files (unit tests).


    On Linux, the path to lib folder is needed to build unit tests, but not at runtime. But if I'm not wrong, on Windows, we also need the path where to search DLLs for at runtime. To do this it's possible to add the lib folder in the Windows PATH.

    But I figured out that Qt Creator is able to run an executable without adding the path of Qt DLLs in Windows PATH, or copying Qt DLLs next to the exe. It seems that it's able to add a kind of runtime path to find DLLs, no?

    Is it possible to do that for my project? Or the only two solutions are to output all binaries to the same folder, or add the lib folder to the Windows PATH?

    Sorry for not being very clear about all of that. Thank you for your help!!

  • Hi, while Qt Creator is a fine piece of software, it's no magician. it just does a putenv()** to add the path to your Qt DLLs to the PATH env. variable before launching your .exe file.
    For example, for MSVC2019, it takes the PATH setting from the Build Settings, prepends it with C:\Qt\5.15.2\msvc2019\bin;, then runs the .exe file.

  • Indeed, thanks a lot!!

    I would like to know: currently I'm adding missing paths manually for each exe in run environment (app + each unit tests).
    Is there a way to do it for all the targets or through the .pro file?
    Maybe with LIBS += -LMyPath or QMAKE_RPATHDIR is more suitable?

  • Do you mean how to add paths so that your .exe files can be run from Qt Creator, or adding paths so that your .exe files can be run without Qt Creator?

    In the first case (starting .exe files from Qt Creator) as long as you only have a single version of Qt installed on your PC, there should be no need to add any paths, as I said earlier, Qt Creator adds the path to the Qt dlls for you.

    In the second case (starting .exe files without Qt Creator) then there are 2 ways to do it:

    1. copy the Qt dlls to the same directory that your .exe file is in, either manually or by using the windeployqt.exe program
    2. adding the path to the Qt dlls to the PATH env. variable

    Note: LIBS += -LMyPath has no effect on runtime lookup of .dlls or .so files
    QMAKE_RPATHDIR has no effect on Windows, because Windows does not support adding a list of paths to your .exe/elf file where to find the Qt dlls/so files (works only on Linux and MacOS)
    Also on Linux, QMAKE_RPATHDIR is not needed for the first case (starting .exe/elf files from Qt Creator) since Qt Creator does that automatically for you (by issuing a linker command e.g. -Wl,-rpath,/home/user/Qt/5.15.2/gcc_64/lib)

Log in to reply