Search code examples
visual-studioqtqmake

How to generate a Visual Studio project file when I build my qmake project?


I like to do most development using Qt Creator, and the builds are driven with qmake and jom. I occasionally want to use Visual Studio for debugging. I'd like to know what magic to put into the .pro file to automatically generate a Visual Studio project file during the project build, every time the .pro file changes.


Solution

  • You can have your qmake project set up so that it automatically generates a Visual Studio project file on every build.

    The below assumes that the base name of the .pro file is the same as the TARGET. Say, if you have TARGET = myapp, you must have it in myapp.pro. Simply add the lines below to your .pro file.

    There is a side effect: every change to the .pro file forces re-linking of the executable.

    The script below supports generating your Visual Studio project no matter what your target mkspec is. Thus it can be generated whether you build on Windows or Unix, and whether you build for Visual Studio on Windows, or using some other compiler

    win32-msvc* {
        # Works when you build for Visual Studio already
        vsproj.spec = $$basename(QMAKESPEC)
    } else {
        # Works when you're not building for Visual Studio (say, using mingw)
        # The configuration you want to build the VS project for (win32-msvc[2005|2008|2010|2012])
        vsproj.spec = win32-msvc2008
    }
    # Sets the project file name appropriately to selected Visual Studio version.
    contains(vsproj.spec, win32-msvc201): vsproj.target = $${TARGET}.vcxproj
    else: vsproj.target = $${TARGET}.vcproj
    # The qmake command to make the Visual Studio project file
    vsproj.commands = qmake -tp vc $${_PRO_FILE_} -spec $${vsproj.spec}
    # The VS project depends on the .pro file
    vsproj.depends = $${_PRO_FILE_}
    # Set the above as a target in the makefile.
    QMAKE_EXTRA_TARGETS += vsproj
    # Make the main target (the executable/library) depend on it,
    # so that it gets built.
    PRE_TARGETDEPS += $${vsproj.target}