摘要:在 MSBuild 中,通过 <PropertyGroup>定义工具路径和参数,再结合 <ItemGroup> 定义需要处理的文件,最后通过<Target>调用工具,即可实现自动化构建。
1 MSBuild相关概念简介
1.1 工程文件
MSBuild工程文件就是XML文件。如下XML片段标明这是一个MSBuild工程文件,所有内容都写在工程标签(Project)当中。
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
</Project>
构建软件时主要需明确两类信息:编译哪些文件和采用什么构建参数。编译哪些文件会在编译项(Item)中给出,构建参数(例如配置项和输出路径)会在编译属性(Property)中给出。
1.2 编译属性(Property)
编译属性就是键值对,都包含在编译属性组标签(PropertyGroup)中,由元素对应,元素属性名就是编译属性名,元素属性值就是编译属性值,如下示例。多个编译属性组标签(PropertyGroup)会被顺序处理。编译属性可以使用"$"引用。
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<DropLocation>
\\sayedData\MSBuildExamples\Drops\$(Configuration)\$(Platform)\
</DropLocation>
</PropertyGroup>
<Target Name="PrepareFilesForDrop">
<Message Text="DropLocation : $(DropLocation)" />
</Target>
</Project>
1.3 编译项(Item)
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<SolutionFile Include="..\InsideMSBuild.sln"/>
</ItemGroup>
<Target Name="PrintSolutionInfo">
<Message Text="SolutionFile: @(SolutionFile)"/>
</Target>
<ItemGroup>
<Compile Include="Form1.cs;Form1.Designer.cs;Program.cs;Properties\AssemblyInfo.cs" />
</ItemGroup>
<!-- Below is the same as the declaration of Compile above -->
<!--<ItemGroup>
<Compile Include="Form1.cs"/>
<Compile Include="Form1.Designer.cs"/>
<Compile Include="Program.cs"/>
<Compile Include="Properties\AssemblyInfo.cs"/>
</ItemGroup>-->
<Target Name="PrintCompileInfo">
<Message Text="Compile: @(Compile)"/>
</Target>
<Target Name="PrintCompileInfo2">
<Message Text="Compile: @(Compile->'%(Filename)')"/>
<Message Text=" "/>
<Message Text ="Compile: %(Compile.Filename)"/>
</Target>
</Project>
编译项放在ItemGroup中,用@引用,如上例所示。
1.4 编译目标(Target)
MSBuild用编译任务(Task)和编译目标(Target)来指示编译操作,如下示例。Message是内置任务,类似的还有Copy、Move、Exec、ResGen和Csc等。
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="HelloWorld">
<Message Text="Hello world!" />
</Target>
</Project>
上面的文本保存到HelloWorld.proj文件中后,打开VS命令行开发工具,可用msbuild [INPUT_FILE] /t:[TARGETS_TO_EXECUTE]命令执行构建操作,如下示例。
msbuild HelloWorld.proj /t:HelloWorld
2 以Qt的VS插件举例
在 MSBuild 中,通过 <PropertyGroup>定义工具路径和参数,再结合 <ItemGroup>
定义需要处理的文件,最后通过 <Target>
调用工具(如 moc
),即可实现自动化构建。以下是更完整的实现逻辑和示例:
2.1 定义工具路径和参数(<PropertyGroup>
)
在 .vcxproj
文件中,通过 <PropertyGroup>
定义 Qt 工具的路径和全局参数:
<PropertyGroup>
<!-- Qt 安装路径(根据实际安装位置修改) -->
<QtInstallPath>C:\Qt\6.5.0\msvc2019_64</QtInstallPath>
<!-- moc 工具路径 -->
<QtMocPath>$(QtInstallPath)\bin\moc.exe</QtMocPath>
<!-- 生成文件的输出目录 -->
<GeneratedFilesDir>$(IntDir)GeneratedFiles\</GeneratedFilesDir>
</PropertyGroup>
2. 2 定义需要处理的文件(<ItemGroup>
)
在 <ItemGroup>
中标记需要 moc
处理的头文件(通常包含 Q_OBJECT
宏):
<ItemGroup>
<!-- 标记需要 moc 处理的头文件 -->
<QtMoc Include="src\MyWidget.h" />
<QtMoc Include="src\MainWindow.h" />
</ItemGroup>
2.3 定义 Target 执行 moc(<Target>
)
通过 <Target>
调用 moc
工具,生成对应的 .cpp
文件:
<Target Name="QtMoc"
BeforeTargets="ClCompile" <!-- 在编译前执行 -->
Inputs="@(QtMoc)" <!-- 输入文件 -->
Outputs="$(GeneratedFilesDir)moc_%(QtMoc.Filename).cpp"> <!-- 输出文件 -->
<!-- 确保输出目录存在 -->
<MakeDir Directories="$(GeneratedFilesDir)" />
<!-- 执行 moc 工具 -->
<Exec Command=""$(QtMocPath)" "%(QtMoc.Identity)" -o "$(GeneratedFilesDir)moc_%(QtMoc.Filename).cpp"" />
<!-- 将生成的 .cpp 文件加入编译列表 -->
<ItemGroup>
<ClCompile Include="$(GeneratedFilesDir)moc_%(QtMoc.Filename).cpp" />
</ItemGroup>
</Target>
2.4 关键机制解析
(1) 执行顺序控制
BeforeTargets="ClCompile"
:确保moc
在编译前执行。Inputs
和Outputs
:MSBuild 会根据文件时间戳判断是否需要重新生成。
(2) 路径处理
$(IntDir)
:指向中间输出目录(如Debug\
或Release\
)。$(GeneratedFilesDir)
:自定义输出目录,避免污染源码。
(3) 动态生成文件名
moc_%(QtMoc.Filename).cpp
:根据输入文件名生成对应的.cpp
文件(如moc_MyWidget.cpp
)。
2.5 完整示例
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- 定义 Qt 工具路径 -->
<PropertyGroup>
<QtInstallPath>C:\Qt\6.5.0\msvc2019_64</QtInstallPath>
<QtMocPath>$(QtInstallPath)\bin\moc.exe</QtMocPath>
<GeneratedFilesDir>$(IntDir)GeneratedFiles\</GeneratedFilesDir>
</PropertyGroup>
<!-- 标记需要 moc 的头文件 -->
<ItemGroup>
<QtMoc Include="src\MyWidget.h" />
<QtMoc Include="src\MainWindow.h" />
</ItemGroup>
<!-- 定义 moc 执行逻辑 -->
<Target Name="QtMoc"
BeforeTargets="ClCompile"
Inputs="@(QtMoc)"
Outputs="$(GeneratedFilesDir)moc_%(QtMoc.Filename).cpp">
<MakeDir Directories="$(GeneratedFilesDir)" />
<Exec Command=""$(QtMocPath)" "%(QtMoc.Identity)" -o "$(GeneratedFilesDir)moc_%(QtMoc.Filename).cpp"" />
<ItemGroup>
<ClCompile Include="$(GeneratedFilesDir)moc_%(QtMoc.Filename).cpp" />
</ItemGroup>
</Target>
<!-- 其他项目配置(如 ClCompile、Link 等) -->
</Project>
2.6 高级用法
(1) 条件配置(Debug/Release)
<PropertyGroup Condition="'$(Configuration)' == 'Debug'">
<QtMocArgs>-DDEBUG -DQT_DEBUG</QtMocArgs>
</PropertyGroup>
<Exec Command=""$(QtMocPath)" $(QtMocArgs) "%(QtMoc.Identity)" -o "$(GeneratedFilesDir)moc_%(QtMoc.Filename).cpp"" />
(2) 多平台支持(x64/Win32)
<PropertyGroup Condition="'$(Platform)' == 'x64'">
<QtInstallPath>C:\Qt\6.5.0\msvc2019_64</QtInstallPath>
</PropertyGroup>
(3) 错误处理
<Exec Command="...">
<Output TaskParameter="ExitCode" PropertyName="MocExitCode" />
</Exec>
<Error Condition="'$(MocExitCode)' != '0'" Text="moc failed with exit code $(MocExitCode)" />
2.7 验证步骤
- 检查输出目录:构建后确认
GeneratedFiles
目录下生成了moc_*.cpp
文件。 - 查看编译列表:确保生成的
.cpp
文件被包含在ClCompile
项中。 - 调试路径问题:如果报错“找不到 moc.exe”,检查
QtInstallPath
是否正确。
通过这种方式,可以将 Qt 的 moc
工具无缝集成到 Visual Studio 的构建流程中,无需手动执行命令。