写在前面

鉴于网上教程可用性不高,为此写下教程.
此教程适合于广大Mac(全平台)用户, Windows用户可选宇宙无敌的VS.(划掉)
(更新) Windows试用llvm和gdb实现调试.
image-20190124204805125

必要的组件

1Xcode command tools
2vsc
3Cpptools
Copy

安装Xcode command tools

终端键入
1xcode-select --install
Copy

安装cpptools

image-20190124205049086

配置VSC

  1. c_cpp_properties.json
    1{
    2    "configurations": [
    3        {
    4            "name": "Mac",
    5            "includePath": [
    6                "${workspaceFolder}/**"
    7            ],
    8            "defines": [],
    9            "macFrameworkPath": [
    10                "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks"
    11            ],
    12            "compilerPath": "/usr/bin/clang",
    13            "cStandard": "c11",
    14            "cppStandard": "c++17",
    15            "intelliSenseMode": "clang-x64"
    16        }
    17    ],
    18    "version": 4
    19}
    Copy
  2. launch.json
    1{
    2    "version": "0.2.0",
    3    "configurations": [
    4        {
    5            "name": "(gdb) Launch",
    6            "type": "cppdbg",
    7            "request": "launch",
    8            "program": "${workspaceRoot}/Debug/${fileBasenameNoExtension}.out",
    9            "args": [],
    10            "stopAtEntry": false,
    11            "preLaunchTask": "build",
    12            "cwd": "${workspaceRoot}",
    13            "environment": [],
    14            "externalConsole": true,
    15            "MIMode": "lldb",
    16           // "miDebuggerPath": "/etc/bin",
    17            "setupCommands": [
    18                {
    19                    "description": "Enable pretty-printing for gdb",
    20                    "text": "-enable-pretty-printing",
    21                    "ignoreFailures": true
    22                }
    23            ]
    24        }
    25    ]
    26}
    Copy
  3. tasks.json
    1{
    2    "version": "2.0.0",
    3    "tasks": [
    4        {
    5            "label": "build",
    6            "command": "clang++",
    7            "type": "shell",
    8            "args": [
    9                "-g","-o","Debug/${fileBasenameNoExtension}.out","${file}",
    10                "-std=c++11",
    11                
    12            ],
    13            "presentation": {
    14                "echo": true,
    15                "reveal": "always",
    16                "focus": false,
    17                "panel": "shared"
    18            }
    19        }
    20    ],
    21}
    Copy
  4. 配置完成 临时文件将保存在Debug文件夹中
  • Windows下的配置

安装LLVM

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

安装MinGW-w64

,安装时注意选择体系架构为x86_64。由于网络原因,你可能不能把它下载下来,经过一点探索,安装程序需要下载一个叫做x86_64-7.1.0-release-posix-seh-rt_v5-rev2的文件,其实我们可以直接在SourceForge上搜到这个,到里面选择第一个下载。下载完成后解压里面的mingw64文件夹中的内容到你安装LLVM的同一个目录合并,合并里面所有文件夹,不会有冲突。
打开终端验证是否能够打开gdb.exe.
Snipaste_2019-01-26_22-00-40

配置vscode

  1. launch.json
    1{
    2    "version": "0.2.0",
    3    "configurations": [
    4        {
    5            "name": "(gdb) Launch in Windows",
    6            "type": "cppdbg",
    7            "request": "launch",
    8            "program": "${workspaceRoot}/${fileBasenameNoExtension}.exe",
    9            "args": [],
    10            "stopAtEntry": false,
    11            "preLaunchTask": "BuildInWindows",
    12            "cwd": "${workspaceRoot}",
    13            "environment": [],
    14            "externalConsole": true,
    15            "MIMode": "gdb",
    16            "miDebuggerPath": "gdb.exe",
    17            "setupCommands": [
    18                {
    19                    "description": "Enable pretty-printing for gdb",
    20                    "text": "-enable-pretty-printing",
    21                    "ignoreFailures": false
    22                }
    23            ]
    24        },
    25       ]
    26}
    Copy
  2. tasks.json
1{
2    "version": "2.0.0",
3    "tasks": [
4   
5        {
6            "label": "BuildInWindows",
7            "command": "clang++",
8            "type": "shell",
9            "args": [
10                "-g","-o","${fileBasenameNoExtension}.exe","${file}",
11                "-std=c++11",
12                
13            ],
14            "presentation": {
15                "echo": true,
16                "reveal": "always",
17                "focus": false,
18                "panel": "shared"
19            }
20        }
21    ],
22}
Copy
Snipaste_2019-01-26_21-59-33
最后成功调试!
Snipaste_2019-01-26_22-00-25

引用

亲亲留个评论再走呗

正在加载评论区...