写在前面

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

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

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

image-20190124204805125

必要的组件

Xcode command tools
vsc
Cpptools

安装Xcode command tools

终端键入

xcode-select --install

安装cpptools

image-20190124205049086

配置VSC

  1. c_cpp_properties.json

    {
        "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

    {
        "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

    {
        "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
    {
        "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
{
    "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++编译器