vs code配置C++运行环境(Mac版)

下载C/C++插件

方法一:通过配置编译文件,将代码编译成可执行文件然后运行

1、在文件根目录下创建.vscode文件夹

​​​​​​​

2、在.vscode文件夹下创建3个json配置文件分别命名为:launch.json、tasks.json、tasks.json

launch.json(C++调试的配置文件)

{
    "version": "0.2.0",
    "configurations": [
        
        {
            "name": "Debug C++ file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "build",
            "miDebuggerPath": "/usr/bin/lldb",  
            "logging": {
                "engineLogging": true
            },
            "visualizerFile": "${workspaceFolder}/gdb-visualizers.xml",
            "internalConsoleOptions": "openOnSessionStart",
            "showDisplayString": true
        }
    ]
}

 task.json(C++编译的配置文件)

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "build",
            "command": "/usr/bin/clang++",  
            "args": [
                "-std=c++11",  
                "-g",  
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": ["$gcc"]
        }
    ]
}

c_cpp_properties.json (这个配置文件是 VS Code 的 C/C++ 扩展插件的 c_cpp_properties.json 文件,主要用于配置项目的 IntelliSense、编译器路径、C/C++ 标准等)

{
    "version": 4,
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "cStandard": "c11",
            "cppStandard": "c++11",  
            "intelliSenseMode": "macos-clang-arm64",  
            "compilerPath": "/usr/bin/clang++"  
        }
    ]
}

3、在项目根目录下创建cpp源文件(一下为一个示例)

main.cpp

#include<iostream>
using namespace std;


int main()
{
    cout << "hello world" << endl;
    return 0;
}

按住cmd+shift+B选择编译文件(一般为第一个),编译完成后按任意键结束,重新打开终端,输入:

./main

即可运行成功。 

注:这里的main是你源文件的名字

        编译会生成一些文件,不需要在意

方法二:通过插件code runner运行

以上配置文件不要动

下载这个插件

右键点击run code或者点击右上角三角形:

ps:code runner插件默认支持的是C++98,对于C++11新特性不支持,需要手动更改插件设置。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

silveice

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值