简体   繁体   中英

Visual Studio 2013 and Qt5: QTDIR set too late

Everything works with Qt in MSVC2013, except one thing: the DLLs are not found, because $(QTDIR) is not defined, when the local debugging environment is set.

My debugging environment settings:

PATH=$(QTDIR)\bin%3b$(PATH)

My .user file:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
    <LocalDebuggerEnvironment>PATH=$(QTDIR)\bin%3b$(PATH)</LocalDebuggerEnvironment>
    <QTDIR>C:\Qt\Qt5.4.1\5.4\msvc2013</QTDIR>
    <DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
    <LocalDebuggerEnvironment>PATH="$(QTDIR)\bin%3b$(PATH)</LocalDebuggerEnvironment>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
    <LocalDebuggerEnvironment>PATH=$(QTDIR)\bin%3b"$(QTDIR)\bin%3b$(PATH)</LocalDebuggerEnvironment>
    <QTDIR>C:\Qt\Qt5.4.1\5.4\msvc2013</QTDIR>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
    <LocalDebuggerEnvironment>PATH="$(QTDIR)\bin%3b$(PATH)</LocalDebuggerEnvironment>
  </PropertyGroup>
</Project>

I don't know why, but the $(QTDIR) variable is not available to LocalDebuggerEnvironment.

It works with following debugging environment settings:

PATH=C:\Qt\Qt5.4.1\5.4\msvc2013\bin%3b$(PATH)

Is it possible to make Visual Studio handle this properly or do I have to enter the path manually?

It seems Visual Studio parses the lines from top to bottom, so with your code...

<LocalDebuggerEnvironment>PATH=$(QTDIR)\bin%3b$(PATH)</LocalDebuggerEnvironment>
<QTDIR>C:\Qt\Qt5.4.1\5.4\msvc2013</QTDIR>

...the variable $(QTDIR) is defined in the second line, and is cannot be used in the first line.

Simply switch the order so that the variable is defined before it is used:

<QTDIR>C:\Qt\Qt5.4.1\5.4\msvc2013</QTDIR>
<LocalDebuggerEnvironment>PATH=$(QTDIR)\bin%3b$(PATH)</LocalDebuggerEnvironment>

Note: Visual Studio only reads the .user file at startup, so you need to (re-)start Visual Studio after editing the file.

Thank you for your discussion of this issue. I can report that it still is extant in Qt 5.8 / QT Extension for Windows Version 2.1.1 / Visual Studio 2015. The symptoms are that entering "$(QTDIR)\\bin" (the location of the Qt DLLs) as a PATH folder spec in the VStudio Project property Debugger...Environment appears to work, but actually does not.

By "appears to work" I mean that if you click on the option to examine the Debugger Environment property, and the raw form of the Environment property looks like "PATH=$(QTDIR)\\bin;...;$(PATH)" the VStudio editing dialog box will helpfully inform you that that PATH string evaluates to (eg) "PATH=D:\\TechApps\\Qt\\5.8\\msvc2015_64b\\bin;..."--just as it should if the QTDIR macro was really present and had the correct value. This is cruelly deceptive!

Because, by "really doesn't work" I mean that the PATH as actually prepared for use when launching your Qt-dependent application from within VStudio doesn't see this macro. Your beautiful PATH string is reduced to "\\bin;...". You can confirm this by temporarily copying your Qt DLLs from $(QTDIR)\\bin into the folder of your application executable. Your application will launch correctly. And if you examine your PATH from within your program (using eg getenv("PATH") in c++, you will see that your program's PATH string is missing QTDIR. (That is, it is what it would be if QTDIR were the empty string.)

FWIW, another symptom of this problem is that if you examine the VStudio list of macros, $(QTDIR) is not in the list.

The solution described above (rearranging the elements in ...vcxproj.user) appears to solve the problem. When you move the definition(s) of QTDIR earlier in that file, the PATH string actually available to the application is correct, and $(QTDIR) is present in the list of macros known to Visual Studio.

HOWEVER, I don't know whether changing the "Qt VS Tools" options, or the "Qt Project Settings," in VStudio causes the .user file to be incorrectly overwritten again.

Changing (or re-selecting) the Qt Version in the solution (Change Solutions Qt Version) fixed the issue for me.

It took many seconds to re-initialize the projects and even then I don't believe the .user file updated until I did a build of the project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM