VsCode & C/C++ & Mac 环境配置

本流程主要参考:官方文档

配置 tasks.json

task.json 的主要作用 配置 编译 的相关参数

配置方式:

  • user setting:shfit+commond+p => >Tasks:Open User Tasks

  • workspace setting:shfit+commond+p => Tasks:Configure Task 前提,没有设置 user

具体的配置文件如下,包含了第三方库的设置

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
28
29
30
31
32
33
34
35
36
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-std=c++17",
"-stdlib=libc++",
"-g",
"${workspaceFolder}/*.cpp",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-I", // 设置 include 头文件路径
"${workspaceFolder}/include",
"-L", // 设置 lib 库路径
"${workspaceFolder}/library",
"-l", // 设置 liblua.a 静态库
"lua"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}

配置 launch.json

launch.json 主要作用 配置 调试的相关参数

配置方式:

  • user setting:

    shfit+commond+p => Preferences:Open User Settings 配置 launch

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    "launch": {
    "version": "0.2.0",
    "configurations": [
    {
    "name": "clang++ - user",
    "type": "cppdbg",
    "request": "launch",
    "program": "${fileDirname}/${fileBasenameNoExtension}",
    "args": [],
    "stopAtEntry": true,
    "cwd": "${workspaceFolder}",
    "environment": [],
    "externalConsole": false,
    "MIMode": "lldb",
    "preLaunchTask": "clang++ build active file" // 先通过 Task 编译成可执行二进制文件再调试
    },
    ]
    },
  • workspace setting:

    直接在 debug 页签下添加单独的 launch.json

配置第三方库代码提示

配置方式:

  • user setting :

    shfit+commond+p => Preferences:Open User Settings

    1
    2
    3
    4
    // 第三方库头文件位置设置
    "C_Cpp.default.includePath": [
    "${workspaceFolder}/include"
    ],
  • workspace setting :

    配置文件 c_cpp_properties.json

    shfit+commond+p => C/C++: Edit Configurations (UI)

    设置 includePath 属性

  • 补充 如果安装了 插件 C Clang Commond Adapter ,需要再 setting 中配置第三方库头文件的位置

    1
    2
    3
    4
    5
    6
    7
    8
    9
       // C Clang Commond Adapter 插件设置第三方库头文件位置
    "clang.cflags": [
    "-I",
    "${workspaceRoot}/include"
    ],
    "clang.cxxflags": [
    "-I",
    "${workspaceRoot}/include"
    ]

扩展

Cmake 工具可以取代 tasks.json 步骤