stay hungry stay foolish

vscode搭建mac环境下c/c++开发环境

第一步

安装 C/C++ 扩展

第二步

选中项目中的C文件后,打开控制面板,使用C/C++:Edit Configurations命令自动生成c_cpp_properties.json文件

c_cpp_properties.json:

{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "macFrameworkPath": [
                "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
            ],
            "compilerPath": "/usr/bin/clang",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "macos-clang-arm64"
        }
    ],
    "version": 4
}

第三步

选中项目中的C文件后,打开控制面板,使用C/C++:Add Debug Configurations命令自动生成tasks.jsonlaunch.json文件

tasks.json里面包含了编译任务,launch.json里面则是用来启动调试。

tasks.json:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: clang build active file",
            "command": "/usr/bin/clang",
            "args": [
                "-fcolor-diagnostics",
                "-fansi-escape-codes",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

launch.js:

{
    "configurations": [
        {
            "name": "C/C++: clang build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "lldb",
            "preLaunchTask": "C/C++: clang build active file"
        }
    ],
    "version": "2.0.0"
}

"preLaunchTask"为启动调试时预先要处理的任务,也就是上面的编译任务,"program"为编译后生成的可执行文件的路径。

最终项目目录结构为:

调试

在行号处打上断点,启动调试,程序就会在断点出停住,此时可以查看变量的值