【Visual Studio】msbuild 使用举例

MSBuild 命令参数详细解析

bash 复制代码
msbuild "MyDll.vcxproj" /p:Configuration=Release /p:Platform=Win32 /t:Rebuild /v:normal /fl /flp:LogFile=.\MyDll_win32_2017_r.txt;verbosity=normal

📋 参数逐个详解

1. "MyDll.vcxproj" - 项目文件

  • 含义:指定要编译的 Visual Studio C++ 项目文件
  • 说明 :
    • .vcxproj 是 Visual Studio C++ 项目文件格式(XML 结构)
    • 包含所有编译配置、源文件列表、头文件路径、库依赖等
    • MSBuild 会读取此文件并执行编译任务

2. /p:Configuration=Release - 编译配置属性

  • 完整形式 :/property:Configuration=Release
  • 含义 :设置项目编译配置为 Release 模式
  • 说明 :
    • Configuration 是 vcxproj 中定义的编译配置名称
    • Release:发布版本,启用优化,不包含调试信息
    • Debug:调试版本,包含调试符号,不优化,便于调试

对应 vcxproj 中的配置:

xml 复制代码
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
  <!-- Release 配置 -->
</PropertyGroup>

常见配置值:

  • Release - 发布版本(优化)
  • Debug - 调试版本(未优化)
  • RelWithDebInfo - 带调试信息的发布版本

3. /p:Platform=Win32 - 目标平台属性

  • 完整形式 :/property:Platform=Win32
  • 含义 :设置编译目标平台为 32 位 Windows
  • 说明 :
    • Win32:32 位 x86 架构
    • x64:64 位 x86-64 架构
    • ARM:32 位 ARM 架构
    • ARM64:64 位 ARM 架构

组合使用:

bash 复制代码
# 32位 Release
/p:Configuration=Release /p:Platform=Win32

# 64位 Debug
/p:Configuration=Debug /p:Platform=x64

# 64位 Release
/p:Configuration=Release /p:Platform=x64

4. /t:Rebuild - 编译目标(Task)

  • 完整形式 :/target:Rebuild
  • 含义 :执行 重新编译 操作
  • 说明 :
    • Rebuild = Clean(清理) + Build(编译)
    • 会删除所有中间文件(.obj, .pdb 等)并完全重新编译
    • 适用于配置更改后确保所有文件都用新配置编译

常用目标对比:

目标 英文全称 行为 使用场景
Build 编译 增量编译,只编译修改过的文件 日常开发
Rebuild 重新编译 先清理再完全编译 配置更改、发布前
Clean 清理 删除所有编译输出 清理项目
Link 链接 仅执行链接步骤 快速测试链接

5. /v:normal - 控制台日志详细程度

  • 完整形式 :/verbosity:normal
  • 含义 :设置控制台输出 的详细程度为 normal
  • 说明:控制 MSBuild 在控制台显示多少信息

所有详细程度级别:

级别 输出内容 适用场景 输出量
quiet 仅错误信息 自动化脚本,CI/CD ⭐
minimal 错误 + 警告 + 简要进度 日常快速查看 ⭐⭐
normal 标准编译信息(文件列表、警告、错误、统计) 常规使用(推荐) ⭐⭐⭐
detailed 详细信息(属性、任务执行过程) 排查构建问题 ⭐⭐⭐⭐
diagnostic 诊断级别(所有信息,包括程序集加载) MSBuild 本身调试 ⭐⭐⭐⭐⭐

normal 级别包含的内容:

  • ✅ 正在编译的文件名(如 audio_transport.cpp)
  • ✅ 编译警告(如 warning C4244)
  • ✅ 编译错误
  • ✅ 链接器输出
  • ✅ 编译结果统计(X 个警告,0 个错误)
  • ✅ 编译用时
  • ❌ 不包含属性展开过程
  • ❌ 不包含程序集加载信息
  • ❌ 不包含任务执行细节

6. /fl - 启用文件日志记录器

  • 完整形式 :/fileLogger
  • 含义 :启用文件日志记录器,将编译日志输出到文件
  • 说明 :
    • MSBuild 支持多个日志记录器同时工作
    • /fl 启用第一个文件日志记录器
    • 可以同时使用 /fl1, /fl2, /fl3, /fl4(最多 5 个)
    • 默认生成文件名:msbuild.log(当前目录)
    • 需要配合 /flp(FileLoggerParameters)自定义参数

多日志记录器示例:

bash 复制代码
# 同时生成两个日志文件
/fl1 /flp1:LogFile=release.log;verbosity=minimal
/fl2 /flp2:LogFile=release_detailed.log;verbosity=detailed

7. /flp:LogFile=.\MyDll_win32_2017_r.txt;verbosity=normal - 文件日志参数

  • 完整形式 :/fileLoggerParameters:LogFile=.\MyDll_win32_2017_r.txt;verbosity=normal
  • 含义:设置文件日志记录器的参数
  • 参数组成 :用分号 ; 分隔多个参数
7.1 LogFile=.\MyDll_win32_2017_r.txt - 日志文件路径
  • 含义:指定日志文件的保存路径和文件名
  • 说明 :
    • . 表示当前目录
    • 文件名命名规则:项目名_平台_VS版本_配置.txt
      • MyDll - 项目名称
      • win32 - 32 位平台
      • 2017 - Visual Studio 2017
      • r - Release 配置(d 表示 Debug)

路径示例:

bash 复制代码
# 当前目录
LogFile=.\build.log

# 指定子目录
LogFile=.\logs\build.log

# 绝对路径
LogFile=C:\logs\MyDll.log
7.2 verbosity=normal - 文件日志详细程度
  • 含义 :设置文件日志 的详细程度为 normal
  • 说明 :
    • ⚠️ 关键 :如果不指定此参数,文件日志默认使用 detailed 级别
    • 这就是为什么之前日志文件比控制台内容多的原因!
    • 必须显式设置为 normal 才能与控制台(/v:normal)保持一致

🎯 完整命令执行流程

bash 复制代码
msbuild "MyDll.vcxproj" /p:Configuration=Release /p:Platform=Win32 /t:Rebuild /v:normal /fl /flp:LogFile=.\MyDll_win32_2017_r.txt;verbosity=normal

执行步骤:

  1. 📂 加载项目文件 MyDll.vcxproj
  2. ⚙️ 设置属性 :
    • Configuration=Release(发布模式)
    • Platform=Win32(32 位)
  3. 🎯 执行任务 :Rebuild(先清理再编译)
  4. 📺 控制台输出 :使用 normal 详细程度
  5. 📝 文件日志 :
    • 启用文件日志记录器(/fl)
    • 保存到 .\MyDll_win32_2017_r.txt
    • 使用 normal 详细程度(与控制台一致)

📊 参数关系图解

复制代码
┌─────────────────────────────────────────────────────────┐
│                    MSBuild 构建引擎                       │
└──────────────────┬──────────────────────────────────────┘
                   │
         ┌─────────┴─────────┐
         ▼                   ▼
   ┌────────────┐      ┌──────────────┐
   │  控制台输出  │      │  文件日志     │
   │            │      │              │
   │ /v:normal  │      │ verbosity=   │
   │            │      │ normal       │
   └────────────┘      └──────────────┘
         │                   │
         ▼                   ▼
   屏幕显示              .txt 文件
   (normal级别)          (normal级别)
   
   ✅ 内容完全一致!

💡 实际应用场景

场景 1:日常开发(当前使用)

bash 复制代码
msbuild "MyDll.vcxproj" /p:Configuration=Release /p:Platform=Win32 /t:Rebuild /v:normal /fl /flp:LogFile=.\MyDll_win32_2017_r.txt;verbosity=normal

场景 2:快速查看(只看错误和警告)

bash 复制代码
msbuild "MyDll.vcxproj" /p:Configuration=Release /p:Platform=Win32 /t:Rebuild /v:minimal /fl /flp:LogFile=.\build.log;verbosity=minimal

场景 3:排查问题(详细日志)

bash 复制代码
msbuild "MyDll.vcxproj" /p:Configuration=Release /p:Platform=Win32 /t:Rebuild /v:detailed /fl /flp:LogFile=.\build_detailed.log;verbosity=detailed

场景 4:同时生成多个日志

bash 复制代码
msbuild "MyDll.vcxproj" /p:Configuration=Release /p:Platform=Win32 /t:Rebuild /v:minimal /fl /flp:LogFile=.\summary.log;verbosity=minimal /fl2 /flp2:LogFile=.\detailed.log;verbosity=detailed

📋 参数速查表

参数 全称 作用 常用值 默认值
/p: /property: 设置项目属性 Configuration=Release Platform=Win32 -
/t: /target: 指定编译目标 Build, Rebuild, Clean Build
/v: /verbosity: 控制台输出详细度 quiet, minimal, normal, detailed, diagnostic normal
/fl /fileLogger 启用文件日志记录器 无 不启用
/flp: /fileLoggerParameters: 文件日志参数 LogFile=xxx.txt verbosity=normal verbosity=detailed ⚠️

⚠️ 重要注意事项

  1. /fl 的默认 verbosity 是 detailed ,不是 normal!

    • 必须显式指定 verbosity=normal 才能与控制台一致
  2. 控制台和文件日志是独立的:

    • /v: 只控制控制台
    • /flp:verbosity= 只控制文件日志
    • 两者需要分别设置
  3. 分号分隔多个参数:

    • /flp:LogFile=xxx.txt;verbosity=normal(分号分隔)
    • 不要使用空格或逗号
  4. 路径中的 . 表示当前目录:

    • .\mc_webrtc_m74_win32_2017_r.txt = 当前目录下的文件
相关推荐
liufeiyu19764 天前
VS 2026 升级后出现错误: 找不到指定的 SDK“Microsoft.NET.Sdk”
visual studio·vs 2026
霍霍的袁4 天前
【C++】map 和 set 的使用 | 从用法到底层
开发语言·c++·学习·visual studio
2501_915909064 天前
移动端开发工具链怎么组合比较好,iOS、Android、跨端三套配置方案
ide·vscode·ios·objective-c·个人开发·swift·敏捷流程
2501_915918415 天前
iOS编程用什么软件好?主流工具Xcode、AppCode、CodeRunner与新兴快蝎对比
ide·vscode·ios·objective-c·个人开发·swift·敏捷流程
VIP_CQCRE5 天前
在 Visual Studio 里接入 Ace Data Cloud:让 AI 编程助手直接用上 OpenAI 兼容接口
openai·ai编程·开发工具·visual studio·acedatacloud
VIP_CQCRE5 天前
在 Visual Studio 里接入 AI 编程助手:用 Ace Data Cloud 打通 OpenAI 兼容接口与 LMLocal
openai·ai编程·visual studio·acedatacloud·lmlocal
free-elcmacom5 天前
发际线警告!手撕《C陷阱与缺陷》终极 BOSS:signal 函数与 typedef 魔法
c语言·开发语言·visual studio code·visual studio
VIP_CQCRE5 天前
Visual Studio 也能丝滑接入大模型:用 Ace Data Cloud + LMLocal 打造 AI 编程体验
ai·大模型·visual studio·ace data cloud·lmlocal
千谦阙听5 天前
【 C++篇】:模板初阶——泛型编程、函数模板与类模板
开发语言·c++·学习·visual studio
滕州市燕猫虎计算机科技工作室个体工商户6 天前
IDEA:Command line is too long
java·ide·intellij-idea