VSCode纯手工配置C/C++项目

面向大二同学不想用Visual Studio的需求,探索Visual Studio Code平台上单纯利用C/C++纯手动配置的方法,实现Release版本和Debug版本的调试和运行,并指定版本进行调试。

前置依赖项:

  • C/C++1 VSCode扩展
  • 配置文件列表,将下面的代码放置到对应目录即可
图 1-1 配置文件列表
  • 添加源文件,main.cpp中一定要有main函数,不然你得更改配置文件
图 1-2 源文件列表
  • 项目启动,点击启动即可
图1-3 启动项
  • cl.exe无法启动问题
    1.使用脚本打开vscode目录2
    2.使用配置文件配置3
      "windows": {
    	 "options": {
     		"shell": {
        		 "executable": "cmd.exe",
         	"args": [
           	"call",
           	"\"D:/Program Files (x86)/Microsoft Visual Studio/2019/Enterprise/Common7/Tools/VsDevCmd.bat\"", //记得自定义> 自己的VsDevCmd环境
           	"&&"
         	]
          }
       }
    
c_cpp_properties.json
{
  "configurations": [
    {
      "name": "windows-msvc-x64",
      // 头文件目录后面有需要自行更改
      "includePath": ["${workspaceFolder}/**"],
      // 自定义编译器路径
      "compilerPath": "D:/Program Files (x86)/Microsoft Visual Studio/2019/Enterprise/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe",
      "cStandard": "c11",
      "cppStandard": "c++11",
      "intelliSenseMode": "windows-msvc-x64",
      "compilerArgs": [""],
      "defines": ["UNICODE", "_UNICODE", "_CRT_SECURE_NO_WARNINGS"]
    }
  ],
  "version": 4
}

tasks.json
{
  "version": "2.0.0",
  // Windows启动项目预设值
  "windows": {
    "options": {
      "shell": {
        "executable": "cmd.exe",
        "args": [
          "call",
          "\"D:/Program Files (x86)/Microsoft Visual Studio/2019/Enterprise/Common7/Tools/VsDevCmd.bat\"", //记得自定义自己的VsDevCmd环境
          "&&"
        ]
      }
    }
  },
  "tasks": [
    // Debug版本
 	{
      "type": "shell",
      "label": "C/C++:Debug",
      "command": "cl.exe",
      "args": [
        "/Zi",
        "/EHsc",
        "/nologo",
        "/Fe${workspaceFolder}\\build\\Debug\\${fileBasenameNoExtension}.exe",
        "${workspaceFolder}\\source\\*.cpp",
        "${workspaceFolder}\\main.cpp"
      ],
      "options": {
        "cwd": "${workspaceFolder}\\build\\Debug"
      },
      "problemMatcher": ["$msCompile"],
      "group": {
        "kind": "build",
        "isDefault": true
      },
      "detail": "Task generated by Debugger."
    }
    // Release版本
    {
      "type": "shell",
      "label": "C/C++:Release",
      "command": "cl.exe",
      "args": [
        "/Zi",
        "/EHsc",
        "/nologo",
        "/Fe${workspaceFolder}\\build\\Release\\${fileBasenameNoExtension}.exe",
        "/DNDEBUG",
        "${file}",
        "user32.lib",
        "kernel32.lib",
        "shell32.lib"
      ],
      "options": {
        "cwd": "${workspaceFolder}\\build\\Release"
      },
      "problemMatcher": ["$msCompile"],
      "group": {
        "kind": "build",
        "isDefault": true
      },
      "detail": "Task generated by Debugger."
    }
  ]
}
launch.json
{
  "version": "0.2.0",
  "configurations": [
    {
      // 对应cl.exe
      "name": "msvc",
      "type": "cppvsdbg",
      "request": "launch",
      "program": "${workspaceFolder}\\build\\Debug\\${fileBasenameNoExtension}.exe",
      "args": [],
      "stopAtEntry": false,
      "environment": [],
      "console": "integratedTerminal",
      "preLaunchTask": "C/C++:Debug",
      "logging": {
        // 用于消除PDB文件找不到打不开问题,来自于https://none53.hatenablog.com/entry/2019/11/28/vsCode_Cannot_find_or_open_the_PDB_file.
        "moduleLoad": false
      }
    }
  ]
}

  1. https://code.visualstudio.com/docs/languages/cpp ↩︎

  2. https://blog.csdn.net/lichen849/article/details/109626205 ↩︎

  3. https://code.visualstudio.com/docs/cpp/config-msvc#_run-vs-code-outside-the-developer-command-prompt ↩︎

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值