vscode_c++环境配置

vscode_c++环境配置


步骤

  1. 前置物料:

    • 安装 vscode 插件 : c/c++

  2. 安装 mingw-w64 (windows上编译c++的工具). 将并配置 bin 目录到环境变量中

    下载地址: http://mingw-w64.org/doku.php/download/mingw-builds

    如: D:\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin


配置代码提示 c_cpp_properties.json

有安装 vs 的情况下 (推荐)

这种方式可以 stl 库都可以代码提示, mingw-w64 里面的库 则不行

  1. 在 vscode 的配置文件 c_cpp_properties.json 中 include vs的库

    1
    2
    3
    4
    5
    6
    7
    8
    {
    "configurations": [
    {
    "name": "Win32",
    "includePath": [
    "${workspaceFolder}/**",
    "C:/my_install/vs2015/VC/include"
    ],

没有安装 vs 的情况下

  1. 查出 mingw-w64 中需要 include 的头文件目录.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    F:\git_repo\CppLab>gcc -v -E -u c++ -
    Using built-in specs.
    COLLECT_GCC=gcc
    Target: x86_64-w64-mingw32
    ...
    #include <...> search starts here: // 下面那几个目录
    D:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include
    D:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed
    D:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include
    End of search list.
  2. 将这几个 头文件 所在目录丢进 vscode 的配置文件 c_cpp_properties.json

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    "configurations": [
    {
    "name": "Win32",
    "includePath": [ // 丢到这里
    "${workspaceFolder}/**",
    "D:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
    "D:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
    "D:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include"
    ],
    "defines": [

配置 编译 task.json

  1. 在 vscode 的 task.json 中新建一个任务

    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": [
    {
    "label": "mybuild",
    "command": "g++",
    "args": [
    "-g",
    "${file}",
    "-o",
    "${workspaceRoot}/CppLab/CppLab.exe" // 导出编译后的 可执行程序
    ],
    "problemMatcher": {
    "owner": "cpp",
    "fileLocation": [
    "relative",
    "${workspaceRoot}"
    ],
    "pattern": {
    "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
    "file": 1,
    "line": 2,
    "column": 3,
    "severity": 4,
    "message": 5
    }
    },
    "group": {
    "kind": "build",
    "isDefault": true
    }
    }
    ]
    }

配置 启动 launch.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "C++ Launch (GDB)",
"type": "cppdbg",
"request": "launch",
"targetArchitecture": "x86",
"program": "${workspaceRoot}/CppLab/CppLab.exe", // debug 可执行程序
"miDebuggerPath":"gdb.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"externalConsole": true,
"preLaunchTask": "mybuild", // 调用 编译task
// "internalConsoleOptions": "openOnSessionStart",
}
]
}

常见问题

  • 中文乱码

    设置字符集

    1
    2
    3
    4
    5
    int main() {
    system("chcp 65001"); // 防止中文乱码, 设置字符集
    ...
    return 0;
    }