我在构建qmake项目时如何生成Visual Studio项目文件?

时间:2013-09-25 19:36:14

标签: visual-studio qt qmake

我喜欢使用Qt Creator进行大多数开发,并且使用qmake和jom驱动构建。我偶尔会想使用Visual Studio进行调试。我想知道每次.pro文件更改时,在.pro文件中放置什么魔法来在项目构建期间自动生成Visual Studio项目文件。

1 个答案:

答案 0 :(得分:3)

您可以设置qmake项目,以便在每次构建时自动生成Visual Studio项目文件。

以下假设.pro文件的基本名称与TARGET相同。说,如果您有TARGET = myapp,则必须在myapp.pro中拥有它。只需将以下行添加到.pro文件中即可。

有一个副作用:对.pro文件的每次更改都会强制重新链接可执行文件。

以下脚本支持生成Visual Studio项目,无论您的目标mkspec是什么。因此,无论您是在Windows还是Unix上构建,以及是在Windows上构建Visual Studio,还是使用其他编译器,都可以生成它

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}