VSCode实现cmake与msbuild一键操作

方法一(强烈推荐): 使用Code-Runner插件的定制命令功能

  1. 下载Code-Runner插件
  2. cmake.exemsbuild.exe路径加入系统环境变量
  3. 在settings.json配置"code-runner.customCommand"项
  4. 之后在项目CMakeLists.txt窗口或其他源码窗口按Ctrl+Shift+K, 或按F1输入run选择Run Custom Command, 即可启动(无需手动调整路径, cd $dir命令会自动进入窗口活动文件所在项目路径)

"code-runner.customCommand"项配置示例(按需选择一个):

  • 生成Debug版本
"code-runner.customCommand": "cd $dir && mkdir build && cd build && cmake .. && MSBuild ALL_BUILD.vcxproj",
  • 生成Release版本
"code-runner.customCommand": "cd $dir && mkdir build && cd build && cmake .. && MSBuild ALL_BUILD.vcxproj -p:Configuration=Release",
  • 生成Release版本, 移动Release文件夹到项目主目录并进入Release文件夹
"code-runner.customCommand": "cd $dir && mkdir build && cd build && cmake .. && MSBuild ALL_BUILD.vcxproj -p:Configuration=Release && mv Release ../  && cd ../Release",

方法二: 配置tasks.json

情况一: 多个项目, 一次配置, 运行时选择
  1. cmake.exemsbuild.exe路径加入系统环境变量
  2. VSCode打开这些项目文件夹的父文件夹, 新建.vscode文件夹, 按需修改下面代码, 保存为.vscode文件夹下tasks.json
{
    "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后才弹出), 依次执行以下命令:

mkdir build
cd build
cmake ..
MSBuild ALL_BUILD.vcxproj -p:Configuration=Release
情况二: 单个项目配置
  1. cmake.exemsbuild.exe路径加入系统环境变量
  2. VSCode打开项目文件夹, 新建.vscode文件夹, 按需修改下面代码, 保存为.vscode文件夹下tasks.json
{
    "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), 依次执行以下命令:

mkdir build
cd build
cmake ..
MSBuild ALL_BUILD.vcxproj -p:Configuration=Release
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值