VS code 中 C/C++ 环境搭建

本篇文章主要为手把手入门搭建VS code C/C++环境。
本文档基于 VSC官方文档 [Get Started with C++ and Mingw-w64 in Visual Studio Code]。

简纲:

  1. mingw
  2. C/C++ (from Microsoft)
  3. workdir 配置 (launch.json, tasks.json)
  4. helloworld 测试

1. mingw 下载及安装

mingw-w64官网
[由官网跳转] sourceforge 下载链接

2. C/C++ 插件

这个比较简单,是VS code内的核心功能之一。教程较多,直接略过。有需要再做更新。

3. workdir 配置 (launch.json, tasks.json)

整个配置和中最复杂、争议和不同之处的一点...千万不要被吓到。
以下是个人配置 launch.jsontask.json

// launch.json
{
    "version": "0.2.0",
    "configurations": [{
        "name": "g++.exe build and debug active file", 
        "type": "cppdbg", 
        "request": "launch", 
        "program": "${fileDirname}\\exe\\${fileBasenameNoExtension}.exe", 
        "args": [], 
        "stopAtEntry": false, 
        "cwd": "${workspaceFolder}", 
        "environment": [], 
        "externalConsole": false, 
        "MIMode": "gdb", 
        "miDebuggerPath": "C:\\mingw-w64\\x86_64-8.1.0-win32-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe", 
        "setupCommands": [
            {
                "description": "Enable pretty-printing for gdb",
                "text": "-enable-pretty-printing",
                "ignoreFailures": false
            }
        ],
        "preLaunchTask": "g++.exe build active file"
    }]
}
// task.json
{ 
  "version": "2.0.0",
  "tasks": [
    {
      "type": "shell",
      "label": "g++.exe build active file",
      "command": "C:\\mingw-w64\\x86_64-8.1.0-win32-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
      "args": ["-g", "${file}", "-o", "${fileDirname}\\exe\\${fileBasenameNoExtension}.exe"],
      "options": {
        "cwd": "C:\\mingw-w64\\x86_64-8.1.0-win32-seh-rt_v6-rev0\\mingw64\\bin"
      },
      "problemMatcher": ["$gcc"],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}

与一般配置不同的是,cpp文件编译出的exe会保存于workdir下单独的文件夹,例子内的文件夹名为exe,达到分离cpp和exe,方便浏览的作用。

你可能感兴趣的:(VS code 中 C/C++ 环境搭建)