Search code examples
qtqmakeatl

Getting Legacy Qt Project to work with ATL in a machine independent way


I am trying to get a Qt project (that is using ATL to communicate with a COM server) to build on Qt 6.2.

So far, I have gotten it to build successfully (at least on my machine) with the following addition in the .pro file:

INCLUDEPATH += "C:/Program Files/Microsoft Visual Studio/2022/Professional/VC/Tools/MSVC/14.32.31326/atlmfc/include/"
win32:LIBS += "C:/Program Files/Microsoft Visual Studio/2022/Professional/VC/Tools/MSVC/14.32.31326/atlmfc/lib/x64/atls.lib"
  • INCLUDEPATH fixes the compile error saying that it is unable to open ATLComTime.h
  • win32:LIBS obviously adds the required lib to the project

Question: I'd rather not write "C:/Program Files/Microsoft Visual Studio/2022/Professional/VC/Tools/MSVC/14.32.31326/...` in my .pro file -- is there a more portable way of telling qmake where to find the required ATL support?

(Or maybe there even is a setting telling qmake that this is an ATL project?)

If it makes any difference: the same project builds on VS 2022 without the changes above, so I am guessing there is some magic switch that VS knows about.


Solution

  • Based on @Simon's answer, here is what I added to my .pro file:

    VSWHERE = "C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe"
    
    ATLMFC_PATH = $$system($$shell_quote($$VSWHERE) -latest -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationPath)
    ATLMFC_PATH = $$replace(ATLMFC_PATH, \\\\, /)
    
    # ATLMFC_PATH should now be something like "C:/Program Files/Microsoft Visual Studio/2022/Professional"
    
    ATLMFC_PATH = $$ATLMFC_PATH/VC/Tools/MSVC/$$(VCToolsVersion)/atlmfc
    
    INCLUDEPATH += "$$ATLMFC_PATH/include"
    win32:LIBS += "$$ATLMFC_PATH/lib/x64/atls.lib"
    

    We call vswhere.exe (which is found at the same spot on all machines in our team) and get the first half of the desired path. Then we add the remainder that (hopefully) looks the same on upcoming versions and insert the value of environment variable VCToolsVersion, hence the $$(...) -- round brackets.

    Finally we add the obtained path with corresponding extensions to INCLUDEPATH and LIBS.

    This should keep us in business until we finally get to the CMake transition.