VS Code 搭建 C++ 开发环境(Mac 环境)

准备

搭建之前需要先安装 clang,在 Mac 上有两种方法进行 clang 的安装:

  1. 在 AppStore 上安装 Xcodeclang 会在 Xcode 安装时自动安装
  2. 在命令行终端上执行 xcode-select --install 进行安装

安装完可通过在命令行终端上输入 clang -v 验证 clang 安装是否成功

$ clang -v
Apple clang version 11.0.3 (clang-1103.0.32.62)
Target: x86_64-apple-darwin19.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

安装 VS Code

VS Code 插件安装

想要运行 C++ 程序需要先安装 C/C++ 插件,打开 VS Code,进入插件扩展(快捷键 Command + Shift + X ),搜索 C/C++ 插件进行安装
C/C++ 插件
看不懂英文的,可以再安装一个中文插件 Chinese (Simplified) Language Pack for Visual Studio Code,安装方法跟安装 C/C++ 插件一样,把搜索词换成 Chinese 就可以了
Chinese 插件

插件安装完成需要重启 VS Code 使其生效

构建运行第一个 C++ 程序

使用以下步骤创建一个项目目录firstProject,并使用 VS Code 打开

$ mkdir firstProject
$ cd firstProject
$ code .

VSCode 工作界面
创建 main.cpp 文件,文件代码如下

#include <iostream>
using namespace std;

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

Command + Shift + P 打开命令行面板,输入 tasks,选择 Tasks:Configure Task 生成 tasks.json 配置文件命令行面板
这里选择 C/C++:clang build active file 模版,选其他模版也没关系,在下面覆盖也行
命令行面板
VS Code 会自动在 .vscode 目录下生成 tasks.json 配置文件
tasks.json
将文件内容修改为

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "command": "clang++",
            "args": [
                "-o",
                "main", // 执行文件名称
                "main.cpp", // 需要执行的源文件
                "-g",
                "-v"
            ],
            "type": "shell",
            "presentation": {
                "echo": true,
                "reveal": "always",
                "panel": "shared"
            },
            "problemMatcher": {
                "owner": "cpp",
                "fileLocation": [
                    "relative",
                    "${workspaceRoot}"
                ],
                "pattern": {
                    "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
            }
        }
    ]
}

接下来按 Command + Shift + B 对源文件进行编译,编译完成后,会在项目路径下生成 main 文件,使用 ./main 即可执行

$ ./main
hello world!

项目调试

如果需要对项目进行进行 debug,则需要先生成 launch.json 文件并对其做相应的配置

  • Command + Shift + D 进入到运行界面

在这里插入图片描述

  • 点击 创建 launch.json 文件,选择 C++(GDB/LLDB)

在这里插入图片描述
同样的,会在 .vscode 文件夹下创建 launch.json 文件

在这里插入图片描述
修改 launch.json 文件内容为

{
    "version": "2.0.0",
    "configurations": [
        {
            "name": "C++ Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceRoot}/main", // main 跟 tasks.json 中的执行文件名称配置一致
            "preLaunchTask": "build",
            "internalConsoleOptions": "openOnSessionStart",
            "logging": {
                "moduleLoad": false,
                "programOutput": true,
                "trace": false
            },
            "showDisplayString": false,
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceRoot}",
            "environment": [],
            "externalConsole": false, // set true to enable keyboard input
            "osx": {
                "MIMode": "lldb"
            }
        }
    ]
}

cout 语句打上断点,然后点击运行按钮即可进行 debug
debug界面
参考:Build and Debug C++ on Visual Studio Code for Mac

到此,使用 VS Code 进行 C++ 项目环境的搭建就完成啦,如果觉得对你有帮助的话可以关注我的公众号huangxy,不定时分享一些技术文章
微信搜索huangxy

  • 9
    点赞
  • 60
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值