【无标题】

标题-VS Code for Mac c++配置环境-参考官方文档

VS Code for Mac c++配置环境-参考官方文档

环境准备

软件环境

1.系统:Mac OS Sonoma 14.2

2.vs code 1.85

3.Xcode

插件下载

1.chinese 汉化

2.c/c++

3.Code Runner

检查clang

在终端输入

clang --version

如果没有安装,安装Xcode

xcode-select --install

创建文件夹和文件

1.选择磁盘,创建一个专用的文件夹,从VScode中打开文件夹

2.在文件夹下创建cpp文件

3.在新建的cpp文件中粘贴官方文档代码进行验证

cpp 复制代码
#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
    vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};

    for (const string& word : msg)
    {
        cout << word << " ";
    }
    cout << endl;
}

配置文件

tasks.json文件

1.保存文件

2.点击运行c/c++文件,选择如图所示的。如果未弹出,按F5,然后如下图所示。会自动生成tasks.json文件

选择第一个

选择第一个c++(gdb/lldb)

选择第二个,clang++

编译成功后,会成功输出

配置tasks.json文件,以下是我的配置文件

cpp 复制代码
{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: clang++ 生成活动文件",
            "command": "/usr/bin/clang++",
            "args": [
                "-std=c++17",
                "-fcolor-diagnostics",
                "-fansi-escape-codes",
                "-g",
                "${file}",
                // "${workspaceFolder}/*.cpp" // 多cpp文件编译
                "-o",
                // "${fileDirname}/${fileBasenameNoExtension}" // 生成文件路径
                "${workspaceFolder}/build/${fileBasenameNoExtension}"
                // "-fcolor-diagnostics",
                // "-fansi-escape-codes",
                // "-g",
                // "${file}",
                // "-o",
                // "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ],
    "version": "2.0.0"
}

launch.json

cpp 复制代码
{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C/C++: clang++ build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            // 可执行文件,和task.json中的-o对应的可执行文件输出路径一致
            "program": "${workspaceFolder}/build/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": true,
            "cwd": "${workspaceFolder}",
            "environment": [],
            // "externalConsole": true,
            "externalConsole": false,
            "MIMode": "lldb",
            "preLaunchTask": "C/C++: clang++ 生成活动文件"
        }
    ]
}

c_cpp_properties.json

按F1,搜索

cpp 复制代码
{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/**"
                // "/Library/Developer/CommandLineTools/usr/include",
                // "/Library/Developer/CommandLineTools/usr/lib/clang/15.0.0/include",
                // // 请根据你的clang版本修改,我这里是11.0.3
                // "/usr/local/include"
            ],
            "defines": [],
            "macFrameworkPath": [
                "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
                // "/Library/Frameworks"
            ],
            "compilerPath": "/usr/bin/clang++",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "macos-clang-arm64"
        }
    ],
    "version": 4
}

验证

运行代码,正常输出

设置断点,进行debug调试

关于调试的问题

如果需要进行cin操作,vscode内的终端无法输入,目前的解决办法是将launch.json文件中的将"externalConsole": false,中的false改为true。即可调用外部终端

相关推荐
Forget the Dream1 小时前
设计模式之责任链模式
java·c++·设计模式·责任链模式
停止重构1 小时前
【开源】低代码 C++程序框架,Linux多线程程序
c++·低代码·多线程·开源框架·云计算引擎
Duramentee1 小时前
C++ 设计模式 十二:责任链模式 (读书 现代c++设计模式)
c++·设计模式·责任链模式
_GR1 小时前
Qt开发⑧Qt的窗口_下_浮动窗口+对话框
开发语言·css·c++·qt·microsoft
萌の鱼1 小时前
leetcode 3183. 达到总和的方法数量
数据结构·c++·算法·leetcode
吃汤圆的抹香鲸1 小时前
GoLand 安装包 绿色版 Win,Mac,Linux 包含IntelliJ全家桶 专为Go语言设计的集成开发环境(IDE)
linux·windows·macos·go·intellij-idea·go1.19
Forget the Dream2 小时前
设计模式之代理模式
java·c++·设计模式·代理模式
吃汤圆的抹香鲸2 小时前
Rider 安装包 绿色版 Win/Mac/Linux 适合.NET和游戏开发者使用 2025全栈开发终极指南:从零配置到企业级实战
linux·运维·windows·sql·游戏·macos·.net
攻城狮7号4 小时前
【第十节】C++设计模式(结构型模式)-Flyweight( 享元)模式
c++·设计模式·享元模式
Forget the Dream4 小时前
设计模式之享元模式
java·c++·设计模式·享元模式