Compiling Qt projects in Qt/MsBuild format without Qt VS Tools installed

cbuchart picture cbuchart · Jun 28, 2018 · Viewed 7.3k times · Source

I have many Qt projects in Visual Studio, using the new Qt/MsBuild format provided by the Qt VS Tools. When compiling in my development environment, where I have the Qt VS Tools installed, everything works flawlessly (compiling from IDE and from command line).

We have a computer dedicated to nightly builds, where only the compiler and msbuild are available (no IDE nor Qt VS Tools are installed).

When compiling the projects in such computer we get an error:

QtMsBuild: could not locate qt.targets, qt.props; project may not build correctly.

Followed by several lines such as

e:********\Preferences.h(4): fatal error C1083: Cannot open include file: 'ui_Preferences.h': No such file or directory

(Project contains Preferences.ui).

How can I solve such errors when Qt VS Tools are not installed?

Answer

cbuchart picture cbuchart · Jun 28, 2018

One solution provided by the Tools' creators is to copy %LOCALAPPDATA%\QtMsBuild into each project directory. But we are talking about hundred of projects. Doing manually, and more on, pushing them as part of the project itself doesn't sound very elegant.

One option would be to add a pre-build step that copies it from a common place into each project (and adding a **/QtMsBuild line to each .gitignore file). Again, looks like too much work.

When looking at the .vcxproj file for the Qt project you find this fragment (the reason for the solution provided by creators):

<PropertyGroup Condition="'$(QtMsBuild)'=='' or !Exists('$(QtMsBuild)\qt.targets')">
  <QtMsBuild>$(MSBuildProjectDirectory)\QtMsBuild</QtMsBuild>
</PropertyGroup>

So, the simplest solution (without being able to install the tools), is to copy the %LOCALAPPDATA\QtMsBuild directory (from a system with the Tools installed) into the night computer (in any common place, but I decided to keep the location used by the tools) and then setting an environment variable:

set QtMsBuild=%LOCALAPPDATA%\QtMsBuild

PS: do not add double quotes to the variable (at least I had problems with them, so VS couldn't find the files).

Update 9-14-2020

I'm not sure on which version it started, but Qt projects created with (at least) the v2.5.2 Qt VS Tools fails to compile indicating that the Qt version has not been set. To solve so, you can

  1. Copy the Registry entries from a computer with tools installed, located at HKCU\SOFTWARE\Digia\Versions.
  2. If you will rely on a single Qt version (but that may be update globally for all projects), you can skip the Registry and just set the Qt version of all projects to $(DefaultQtVersion) (the same used in past project formats) and define an environment variable pointing to the directory of the version: set DefaultQtVersion=c:\Qt\Qt_5_15_0\Win32, for example.