【VScode C++环境配置 (MAC)】

vscode下载
官网下载 Visual Studio Code - Code Editing. Redefined
在这里插入图片描述

扩展包安装
C/C++

CodeLLDB

CodeLLDB 可能由于网络问题安装不成功,可手动下载

打开Github:https://github.com/vadimcn/vscode-lldb/releases,打开之后根据你的电脑芯片下载对应的版本

如果是基于Intel的Mac选择codelldb-×86_64-darwin.vsix,

如果是基于Apple Silicon的Mac选择codelldb-aarch64-darwin.vsix

下载完成后,在扩展中点击从vsix中安装,即可。
在这里插入图片描述

首先准备一个cpp文件

#include<iostream>
#include<string>
#include<vector>
using namespace std;

int main(){
    int b = 1;
    // auto a = b;
    // cout << a << endl;
    // vector<string> msg{"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};
    // int len = msg.size();
    // cout << "长度" << len << endl;
    for (int i=0; i<5; i++){
        // ms.push_back("a");
        cout << i << endl;
    }

    return 0;
}

launch.json
直接点击调试,点击运行调试
在这里插入图片描述
选择C++(GDB/LLDB)
在这里插入图片描述

选择g++ -生成和调试活动文件
在这里插入图片描述

在.vscode中会生成两个文件,我们对这两个文件进行配置即可。
在这里插入图片描述

launch.json

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "lldb",
            "request": "launch",
            "name": "C++ debug",   
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "cwd": "${workspaceFolder}",
            "preLaunchTask": "C/C++: g++ 生成活动文件"
        }
    ]
}

在这里插入图片描述
可以看到我们的C++环境基本配置成功

但如果我们将代码做如下更改,就会编译不通过,原因是目前的编译不支持C++11及以后的标准。

#include<iostream>
#include<string>
#include<vector>
using namespace std;

int main(){
    int b = 1;
    auto a = b;
    cout << a << endl;
    vector<string> msg{"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};
    int len = msg.size();
    cout << "长度" << len << endl;
    for (int i=0; i<5; i++){
        // ms.push_back("a");
        cout << i << endl;
    }

    return 0;
}

所以还需要进行其他配置。

task.json

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ 生成活动文件",
            "command": "/usr/bin/g++",
            "args": [
                "-std=c++17",
                "-stdlib=libc++",
                "-fdiagnostics-color=always",
                "-g",
                "-Wall",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ],
    "version": "2.0.0"
}

c_cpp_properties.json(通过command+shift+p打开,如下图找到:)
在这里插入图片描述

{
    "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++17",
            "intelliSenseMode": "macos-clang-x64"
        }
    ],
    "version": 4
}

至此,我们的C++环境已经全部配置完成。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值