0

Already explored couple of similar solutions in SO, none of them seems to work.

I created two wrapper scripts, changing PATH env for x86 and x64 versions of Qt, only for the period of debugging the application of specific application architecture.

For example, choosing x86 configuration, I need to add path to msvc2015\bin and msvc2015\plugins\platforms to be able to debug x86 Qt applications. For x64 it would be msvc2015_64\bin and msvc2015_64\plugins\platforms respectively.

As a first version I created script setup_path.cmd and executed it as a pre-build step

setup_path.cmd

@echo off

set PATH=%PATH%;C:\Qt\5.11.2\msvc2015\bin;C:\Qt\5.11.2\msvc2015\plugins\platforms

...and called it as a pre-build step from CMakeLists.txt

add_custom_command(TARGET ${TARGET} PRE_BUILD COMMAND cmd /c ${CMAKE_CURRENT_SOURCE_DIR}/setup_path.cmd)

The path ${CMAKE_CURRENT_SOURCE_DIR} does not contain spaces.

The scripts executed normally, but Qt application under the debugger still does not see Qt libraries and plugins.

I choose way editing PATH as the most obvious, probably there is more specific way for Qt, but I could not easily find in the documentation.

1 Answers1

0

This is because the build environment is not the same as run/debug environment. Modifying PATH for build environment will not affect your application run/debug environment (at least not for CMake).

The problem is that you need to tell Windows where to get the Qt's DLLs from. The DLL lookup is done by windows in 4 main steps in this order (more here):

  1. The directory from which the application loaded
  2. The current directory
  3. The system directories
  4. The directories that are listed in the PATH environment variable

Modifying windows system folders is really not something you want to do in this case so we are left with other three options.

  1. Copy necessary DLLs into application directory. This approach is normally taken by the application developers when they want to have "portable" version of the software that does not depend on users having to install Qt. This can be achieved with windeployqt or BundleUtilities.

  2. Have necessary DLLs in the current working folder. Copy DLLs to your working directory. The working directory in Visual Studio is defined in Properties of your project -> Configuration Properties -> Debugging -> Working Directory and is by default set to $(ProjectDir).

  3. Have path to DLL added to the environment variable PATH when running your debugger. Visual Studio has Properties -> Configuration Properties -> Debugging -> Environment field which allows you to modify the environment. Unfortunately CMake does not have facilities to do it for you. But we can approach it from another way - by modifying the Properties -> Configuration Properties -> VC++ Directories -> Executable Directories. CMake starting from version 3.12 has variable CMAKE_VS_SDK_EXECUTABLE_DIRECTORIES to access it.


Also check this answer

Teivaz
  • 5,462
  • 4
  • 37
  • 75