方法一(强烈推荐): 使用Code-Runner插件的定制命令功能
- 下载Code-Runner插件
- 将
cmake.exe
和msbuild.exe
路径加入系统环境变量 - 在settings.json配置"code-runner.customCommand"项
- 之后在项目CMakeLists.txt窗口或其他源码窗口按
Ctrl+Shift+K
, 或按F1
输入run
选择Run Custom Command
, 即可启动(无需手动调整路径,cd $dir
命令会自动进入窗口活动文件所在项目路径)
"code-runner.customCommand"项配置示例(按需选择一个):
- 生成Debug版本
复制代码
1
2"code-runner.customCommand": "cd $dir && mkdir build && cd build && cmake .. && MSBuild ALL_BUILD.vcxproj",
- 生成Release版本
复制代码
1
2"code-runner.customCommand": "cd $dir && mkdir build && cd build && cmake .. && MSBuild ALL_BUILD.vcxproj -p:Configuration=Release",
- 生成Release版本, 移动Release文件夹到项目主目录并进入Release文件夹
复制代码
1
2
3"code-runner.customCommand": "cd $dir && mkdir build && cd build && cmake .. && MSBuild ALL_BUILD.vcxproj -p:Configuration=Release && mv Release ../ && cd ../Release",
方法二: 配置tasks.json
情况一: 多个项目, 一次配置, 运行时选择
- 将
cmake.exe
和msbuild.exe
路径加入系统环境变量 - VSCode打开这些项目文件夹的父文件夹, 新建
.vscode
文件夹, 按需修改下面代码, 保存为.vscode
文件夹下tasks.json
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27{ "version": "2.0.0", "tasks": [ { "label": "Build", //名称, 可自定义 "type": "shell", "command": "mkdir build; cd build; cmake ..; MSBuild ALL_BUILD.vcxproj -p:Configuration=Release", // 选择 Debug or Release "options": { "cwd": "${input:projectName}" }, "group": { "kind": "build", "isDefault": true // 不作为默认改为false } } ], "inputs": [ { "id": "projectName", "description": "The name of the project", "default": "", "type": "promptString" } ] }
Ctrl+Shift+B
一键启动, 然后在弹出输入框输入所选项目文件夹名称(如果不作为默认需要再单击选择该task后才弹出), 依次执行以下命令:
复制代码
1
2
3
4
5mkdir build cd build cmake .. MSBuild ALL_BUILD.vcxproj -p:Configuration=Release
情况二: 单个项目配置
- 将
cmake.exe
和msbuild.exe
路径加入系统环境变量 - VSCode打开项目文件夹, 新建
.vscode
文件夹, 按需修改下面代码, 保存为.vscode
文件夹下tasks.json
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16{ "version": "2.0.0", "tasks": [ { "label": "Build", //名称, 可自定义 "type": "shell", "command": "mkdir build; cd build; cmake ..; MSBuild ALL_BUILD.vcxproj -p:Configuration=Release", // 选择 Debug or Release "group": { "kind": "build", "isDefault": true // 不作为默认改为false } } ] }
Ctrl+Shift+B
一键启动,(如果不作为默认需要再单击选择该task), 依次执行以下命令:
复制代码
1
2
3
4
5mkdir build cd build cmake .. MSBuild ALL_BUILD.vcxproj -p:Configuration=Release
最后
以上就是坚强发卡最近收集整理的关于VSCode实现cmake与msbuild一键操作的全部内容,更多相关VSCode实现cmake与msbuild一键操作内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复