在mac上使用vscode创建第一个C++项目

准备工作:

  • 安装好vscode
  • 安装插件『C/C++』

正式开始:

首先是创建一个空的文件夹(比如文件夹为test),然后在其中新建一个.cpp文件(比如文件为hello.cpp)打开vscode打开test文件夹作为工作目录,接下来用三步配置好C++开发环境

第一步:

[⇧⌘P]打开命令模式,选择[C/Cpp: Edit Configurations(JSON)]命令,回车后会自动生成一个.vscode目录,目录下有一个c_cpp_properties.json文件,下面给出我的文件示例:

{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/**",
                "/Library/Developer/CommandLineTools/usr/include/c++/v1",
                "/usr/local/include",
                "/Library/Developer/CommandLineTools/usr/lib/clang/11.0.0/include",
                "/Library/Developer/CommandLineTools/usr/include"
            ],
            "defines": [],
            "macFrameworkPath": [
                "/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/System/Library/Frameworks",
                "/System/Library/Frameworks",
                "/Library/Frameworks"
            ],
            "compilerPath": "/usr/bin/clang",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}

第二步:

[⇧⌘P]打开命令模式,选择[Tasks: Configure Task]命令,选择的模板为MSBuild,回车后会自动在.vscode目录下生成一个tasks.json文件,下面给出我的文件示例:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build c++",
            "type": "shell",
            "command": "g++",
            "args": [
                "${file}",
                "-std=c++17",
                "-g",
                "-Wall",
                "-lm",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}.out"
            ],
            "group": "build",
            "presentation": {
                "reveal": "silent",
                "panel": "shared",
                "echo": true,
                "focus": false,
                "showReuseMessage": true,
                "clear": false
            },
            "problemMatcher": "$gcc"
        },
        {
            "label": "run c++",
            "type": "shell",
            "dependsOn": "build c++",
            "command": "${fileDirname}/${fileBasenameNoExtension}.out",
            "presentation": {
                "focus": true
            },
            "group": "test"
        }
    ]
}

第三步:

[⇧⌘P]打开命令模式,选择[Debug: Open launch.json]命令,选择的模板为C/C++,回车后会自动在.vscode目录下生成一个launch.json文件,下面给出我的文件示例:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "c/c++ Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}.out",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "lldb",
            "preLaunchTask": "build c++",
            "logging": {
                "trace": true,
                "traceResponse": true,
                "engineLogging": true
            }
        }
    ]
}

完成这三步C++开发环境就配置好了,接下来就可以编译,运行,调试C++程序了

[⇧⌘B]是编译程序,[⇧⌘R]是运行程序,如果安装了插件『Code Runner』可以直接运行程序

如果需要调试,那就按F5,进入调试模式即可

好了,这样就可以顺利地在vscode上进行C++开发啦,赶快行动起来吧~

  • 7
    点赞
  • 72
    收藏
    觉得还不错? 一键收藏
  • 11
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值