MacOS下VScode调试C/C++方法

正常编译一个点.c 或者.cpp后缀的文件,都是用命令先编译成二进制文件,然后再执行二进制。VScode也遵循着这两个步骤,用到两个文件,一个是tasks.json和launch.json

第一步,先来配置tasks.json文件

在vscode主界面,按shift+command+p,选择配置任务,就会自动打开一个tasks.json模版:

tasks.json 配置内容如下即可:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Build",
            "type": "shell",
            "command": "clang++",
            "args": [
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}.out",
                "-g"
            ],            
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

配置好之后,此刻使用shift+command+B,就可以进行编译了。

tasks.json中的参数说明:


参数说明:
"label": 任务的名称 (build main)
"type" : 任务的类型,一共有两种(shell/Process),其中shell表示先打开shell,再执行输入命令;process则直接执行命令 (由于编译c++ 需要借助shell上进行执行命令)
"command": 实际上执行的命令(c++ 使用clang++命令进行编译)
"args": 使用clang++编译,其中参数表示的命令: clang++ xxx.c -o xxx.out -g ; 当使用-g 表示c/c++调试必备的一个参数 同时会在目录生成一个xxx.out.dSYM结尾的文件夹

第二步编辑launch.json文件:

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "启动调试",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/${fileBasenameNoExtension}.out",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "lldb"
        }
    ]
}

配置好之后,就可以点击启动调试小按钮了,愉快的debug….

关于launch.json 文件,配置参数说明:

"name": 配置名称,之后会出现再调试窗口的启动配置上
"type": 配置类型(不知道是否可以修改TODO:)
"request": 请求配置类型,可以设置为 launch(启动) 或者 attach(附加)
"program": 进行调试的程序的位置(此处在当前文件夹下的a.out可执行文件)
"stopAtEntry": 设置为true时,程序将会暂停再程序的入口中
"cmd": 当前调试所在的路径
"externalConsole": 调试是否显示控制台窗口,true即显示控制台

当然,有的情况下还会用到一个文件c_cpp_properties.json,配置内容如下:

{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "macFrameworkPath": [
                "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
            ],
            "compilerPath": "/usr/bin/clang",
            "cStandard": "c17",
            "cppStandard": "c++98",
            "intelliSenseMode": "macos-clang-x64"
        }
    ],
    "version": 4
}

——————————–分割线———————————–

备用:完整的包含python,go,C/C++调试方式的settings.json文件:

{
    "workbench.colorTheme": "Solarized Dark",
    "python.defaultInterpreterPath": "/usr/local/bin/python3",
    "security.workspace.trust.untrustedFiles": "open",
    "editor.suggestSelection": "first",
    "vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue",
    "workbench.startupEditor": "none",
    "diffEditor.renderSideBySide": false,
    "code-runner.runInTerminal": true,
    "workbench.editor.enablePreview": false,
    "go.enableCodeLens": {},
    "launch": {
        "configurations": [
            {
                "name": "Python3-Debug",
                "type": "python",
                "request": "launch",
                "program": "${file}",
                "console": "integratedTerminal",
                "args":[]
            },
            {
                "name": "C/C++-Debug",
                "type": "cppdbg",
                "request": "launch",
                "program": "${workspaceFolder}/${fileBasenameNoExtension}.out",
                "stopAtEntry": false,
                "cwd": "${fileDirname}",
                "environment": [],
                "externalConsole": false,
                "MIMode": "lldb",
                "args": []
            },
            {
                "name": "GO-Debug",
                "type": "go",
                "request": "launch",
                "mode": "auto",
                "program": "${file}",
                "args":[]
            }
        ],
    }

}