Mac使用Visual Studio Code开发C++

    xiaoxiao2022-06-24  192

    目录

    在VS Code上使用Clang进行编译和调试

    在VS Code上使用Clang进行编译和调试

    安装Visual Studio Code

    安装C++扩展插件

    安装Clang插件

    在环境变量中添加code命令(如果已经配置了,跳过)

    “Shift+Command+P”打开Command Palette输入“Shell”,选择“Shell Command: Install ‘code’ command in PATH”关闭VS Code

    在一个文件夹中启动VS Code

    步骤mkdir cprojects cd cprojects mkdir cpptest cd cpptest code .

    创建3个json文件

    c_cpp_properties.json指定编译器的路径

    “Shift+Command+P”打开Command Palette输入“C/C++”,选择“Edit Configurations” { "configurations": [ { "name": "macOS", "includePath": [ "${workspaceFolder}/**" ], "defines": [], "macFrameworkPath": [ "/System/Library/Frameworks", "/Library/Frameworks" ], "compilerPath": "/usr/bin/clang", "cStandard": "c11", "cppStandard": "c++17", "intelliSenseMode": "clang-x64" } ], "version": 4 }

    tasks.json指定如何构建可执行文件

    “Shift+Command+P”打开Command Palette输入“task”,选择“Tasks: Add a default build task”->“others { "version": "2.0.0", "tasks": [ { "label": "Build with Clang", "type": "shell", "command": "clang++", "args": [ "-std=c++17", "-stdlib=libc++", "helloworld.cpp", "-o", "helloworld.out", "--debug" ], "group": { "kind": "build", "isDefault": true } } ] }

    launch.json进行调试器配置

    { "version": "0.2.0", "configurations": [ { "name": "(lldb) Launch", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/helloworld.out", "args": [], "stopAtEntry": true, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": true, "MIMode": "lldb", "logging": { "trace": true, "traceResponse": true, "engineLogging": true } } ] }

    helloworld.cpp

    #include<iostream> using namespace std; int main() { cout << "Hello World" <<endl; return 0; }

    编译“Shift+Command+B”

    参考


    最新回复(0)