VSC配置C/C++开发环境正确配置(适用于类Unix/Windows平台)

写在前面

鉴于网上教程可用性不高,为此写下教程.

此教程适合于广大Mac(全平台)用户,Windows用户可选宇宙无敌的VS.(划掉)

(更新) Windows试用llvm和gdb实现调试.

image-20190124204805125

必要的组件

1
2
3
Xcode command tools
vsc
Cpptools

安装Xcode command tools

终端键入

1
xcode-select --install

安装cpptools

image-20190124205049086

配置VSC

  1. c_cpp_properties.json

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    {
    "configurations": [
    {
    "name": "Mac",
    "includePath": [
    "${workspaceFolder}/**"
    ],
    "defines": [],
    "macFrameworkPath": [
    "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks"
    ],
    "compilerPath": "/usr/bin/clang",
    "cStandard": "c11",
    "cppStandard": "c++17",
    "intelliSenseMode": "clang-x64"
    }
    ],
    "version": 4
    }
  2. launch.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
    {
    "version": "0.2.0",
    "configurations": [
    {
    "name": "(gdb) Launch",
    "type": "cppdbg",
    "request": "launch",
    "program": "${workspaceRoot}/Debug/${fileBasenameNoExtension}.out",
    "args": [],
    "stopAtEntry": false,
    "preLaunchTask": "build",
    "cwd": "${workspaceRoot}",
    "environment": [],
    "externalConsole": true,
    "MIMode": "lldb",
    // "miDebuggerPath": "/etc/bin",
    "setupCommands": [
    {
    "description": "Enable pretty-printing for gdb",
    "text": "-enable-pretty-printing",
    "ignoreFailures": true
    }
    ]
    }
    ]
    }
  3. tasks.json

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    {
    "version": "2.0.0",
    "tasks": [
    {
    "label": "build",
    "command": "clang++",
    "type": "shell",
    "args": [
    "-g","-o","Debug/${fileBasenameNoExtension}.out","${file}",
    "-std=c++11",

    ],
    "presentation": {
    "echo": true,
    "reveal": "always",
    "focus": false,
    "panel": "shared"
    }
    }
    ],
    }
  4. 配置完成 临时文件将保存在Debug文件夹中

  • Windows下的配置

安装LLVM

LLVM Download Page,在网页中找到适用于Windows
64位的最新预编译版本,不需要下载sig签名文件。安装过程中注意选择为所有用户安装,这样会为你添加到环境变量。
这两步完成以后打开cmd,输入clang应该可以看到如下输出。

Snipaste_2019-01-26_22-01-06

安装MinGW-w64

MinGW-w64 - for 32 and 64 bit Windows,安装时注意选择体系架构为x86_64。由于网络原因,你可能不能把它下载下来,经过一点探索,安装程序需要下载一个叫做x86_64-7.1.0-release-posix-seh-rt_v5-rev2的文件,其实我们可以直接在SourceForge上搜到这个MinGW-w64 - for 32 and 64 bit Windows,到里面选择第一个下载。下载完成后解压里面的mingw64文件夹中的内容到你安装LLVM的同一个目录合并,合并里面所有文件夹,不会有冲突。

打开终端验证是否能够打开gdb.exe.

Snipaste_2019-01-26_22-00-40

配置vscode

  1. launch.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
    {
    "version": "0.2.0",
    "configurations": [
    {
    "name": "(gdb) Launch in Windows",
    "type": "cppdbg",
    "request": "launch",
    "program": "${workspaceRoot}/${fileBasenameNoExtension}.exe",
    "args": [],
    "stopAtEntry": false,
    "preLaunchTask": "BuildInWindows",
    "cwd": "${workspaceRoot}",
    "environment": [],
    "externalConsole": true,
    "MIMode": "gdb",
    "miDebuggerPath": "gdb.exe",
    "setupCommands": [
    {
    "description": "Enable pretty-printing for gdb",
    "text": "-enable-pretty-printing",
    "ignoreFailures": false
    }
    ]
    },
    ]
    }
  1. tasks.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
"version": "2.0.0",
"tasks": [

{
"label": "BuildInWindows",
"command": "clang++",
"type": "shell",
"args": [
"-g","-o","${fileBasenameNoExtension}.exe","${file}",
"-std=c++11",

],
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared"
}
}
],
}

Snipaste_2019-01-26_21-59-33

最后成功调试!

Snipaste_2019-01-26_22-00-25

引用

在VS Code中使用Clang作为你的C++编译器

本文标题:VSC配置C/C++开发环境正确配置(适用于类Unix/Windows平台)

文章作者:yiny

发布时间:2019年01月24日 - 20:01

最后更新:2019年02月18日 - 19:02

原始链接:https://blog.yiny.ml/2019/01/24/vscode_config->Clang/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

0%