2021-05-18 VSCode中C语言编译和Debug环境配置

配置步骤:

插件安装

image.png

json文件配置

  1. 新建文件夹并在VSCode中选择“打开”
  2. 添加.cpp文件和.vscode文件夹,添加task.json,内容如下:
{
      "version": "2.0.0",
      "tasks": [
            {
                  "label": "Build with Clang",
                  "type": "shell",
                  "command": "clang++",
                  "args": [
                        "${file}",
                        "-std=c++11",
                        "-o",
                        "${workspaceFolder}/out/${fileBasenameNoExtension}.out",
                        "-g",
                        "--debug"
                  ],
                  "group": "build"
            },
            {
                  "type": "cppbuild",
                  "label": "C/C++: clang build active file",
                  "command": "/usr/bin/clang",
                  "args": [
                        "-g",
                        "${file}",
                        "-o",
                        "${fileDirname}/${fileBasenameNoExtension}"
                  ],
                  "options": {
                        "cwd": "${workspaceFolder}"
                  },
                  "problemMatcher": [
                        "$gcc"
                  ],
                  "group": {
                        "kind": "build",
                        "isDefault": true
                  },
                  "detail": "Task generated by Debugger."
            }
      ]
}
  1. 添加c_cpp_properties.json
{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/**",
                "/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1",
                "/Library/Developer/CommandLineTools/usr/lib/clang/12.0.0/include",
                "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include",
                "/Library/Developer/CommandLineTools/usr/include",
                "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
            ],
            "defines": [],
            "macFrameworkPath": [
                "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library"
            ],
            "compilerPath": "/usr/bin/clang",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-arm64"
        }
    ],
    "version": 4
}

其中includePath中路径可以通过终端中输入gcc -v -E -x c++ -获得,具体参考
https://blog.csdn.net/qq_42920429/article/details/106342615

  1. 添加'launch.json'
{
      // 使用 IntelliSense 了解相关属性。 
      // 悬停以查看现有属性的描述。
      // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
      "version": "0.2.0",
      "configurations": [
            {
                  "name": "clang - 生成和调试活动文件",
                  "type": "cppdbg",
                  "request": "launch",
                  "program": "${fileDirname}/${fileBasenameNoExtension}",
                  "args": [],
                  "stopAtEntry": false,
                  "cwd": "${workspaceFolder}",
                  "environment": [],
                  "externalConsole": true,
                  "MIMode": "lldb",
                  "preLaunchTask": "C/C++: clang build active file"
            }
      ]
}

也可以通过在debug中选择创建launch.json文件,选择C++(GDB/LLDB)->clang(c++则选择clang++)

image.png

image.png

或者直接在.cpp文件中Fn+F5也可换出.json文件创建。
若需要运行输出在终端中显示,可以把 "externalConsole": false替换为"externalConsole": true

参考:
>> 视频:配置过程
>> 视频:调试工具的使用

快捷键

编译运行 Control+Option+N
调试Fn+F5
调出命令窗口 Command+Shift+P
>> 所有快捷键

你可能感兴趣的:(2021-05-18 VSCode中C语言编译和Debug环境配置)