跳到主要内容 Mac 上配置 VSCode 的 C/C++ 开发环境 GCC G++ 教程 | 极客日志
C++
Mac 上配置 VSCode 的 C/C++ 开发环境 GCC G++ 教程 介绍在 macOS 系统下配置 VSCode 进行 C/C++ 开发的完整流程。内容包括安装 Xcode Command Line Tools 或 Homebrew GCC,安装必要扩展,配置 c_cpp_properties.json、tasks.json 和 launch.json 文件以实现智能提示、编译和调试功能。此外还涵盖多文件项目配置、Makefile 使用及常见错误解决方案,帮助用户快速搭建稳定的开发环境。
技术博主 发布于 2026/3/30 更新于 2026/4/13 0 浏览前言
作为程序员,一个顺手的开发环境至关重要。VSCode 作为轻量级但功能强大的代码编辑器,配合 GCC/G++ 编译器,能够在 Mac 上提供优秀的 C/C++ 开发体验。本文将详细介绍从零开始的完整配置过程。
一、环境准备:安装编译工具
1.1 安装 Xcode Command Line Tools(推荐首选)
打开终端,执行以下命令:
xcode-select --install
执行后会弹出安装对话框,点击'安装'即可。这个过程会安装 GCC、G++、Make 等基础开发工具。
:
验证安装
gcc --version
g++ --version
1.2 使用 Homebrew 安装更新版本的 GCC(可选) 如果你需要更新版本的 GCC,可以通过 Homebrew 安装:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh) "
brew install gcc
注意 :通过 Homebrew 安装的 GCC 命令可能带有版本号,如 gcc-13、g++-13。
二、VSCode 扩展安装 打开 VSCode,按 Cmd+Shift+X 打开扩展商店,搜索并安装以下扩展:
C/C++ (Microsoft 官方扩展)- 必需
C/C++ Extension Pack (扩展包,包含多个相关工具)- 推荐
三、项目配置详解
3.1 创建项目结构 my_cpp_project/
├── src/
│ └── main.cpp
├── include /
├── .vscode/
│ ├── tasks.json
│ ├── launch.json
│ └── c_cpp_properties.json
└── Makefile
3.2 配置 c_cpp_properties.json(智能提示) 在项目根目录创建 .vscode/c_cpp_properties.json:
{
"configurations" : [
{
"name" : "Mac" ,
"includePath" : [ "${workspaceFolder}/**" , "/usr/local/include" , "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include" ] ,
"defines" : [ ] ,
"macFrameworkPath" : "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks" ,
"compilerPath" : "/usr/bin/gcc" ,
"cStandard" : "c17" ,
"cppStandard" : "c++17" ,
"intelliSenseMode" : "macos-gcc-x64"
}
] ,
"version" : 4
}
如果使用 Homebrew 的 GCC ,修改 compilerPath:
"compilerPath" : "/usr/local/bin/gcc-13"
3.3 配置 tasks.json(编译任务) 创建 .vscode/tasks.json,这是编译配置的核心:
{
"version" : "2.0.0" ,
"tasks" : [
{
"type" : "shell" ,
"label" : "C/C++: g++ build active file" ,
"command" : "/usr/bin/g++" ,
"args" : [ "-std=c++17" , "-g" , "${file}" , "-o" , "${fileDirname}/${fileBasenameNoExtension}" , "-I" , "${workspaceFolder}/include" ] ,
"options" : { "cwd" : "${fileDirname}" } ,
"problemMatcher" : [ "$gcc" ] ,
"group" : { "kind" : "build" , "isDefault" : true }
}
]
}
3.4 配置 launch.json(调试配置) 创建 .vscode/launch.json,配置调试环境:
{
"version" : "0.2.0" ,
"configurations" : [
{
"name" : "C/C++: g++ build and debug active file" ,
"type" : "cppdbg" ,
"request" : "launch" ,
"program" : "${fileDirname}/${fileBasenameNoExtension}" ,
"args" : [ ] ,
"stopAtEntry" : false ,
"cwd" : "${fileDirname}" ,
"environment" : [ ] ,
"externalConsole" : false ,
"MIMode" : "lldb" ,
"preLaunchTask" : "C/C++: g++ build active file"
}
]
}
四、测试环境配置
4.1 创建测试文件 #include <iostream>
using namespace std;
int main () {
cout << "🎉 恭喜!VSCode C++ 环境配置成功!" << endl;
cout << "Hello, VSCode with GCC on Mac!" << endl;
int a = 10 , b = 20 ;
cout << a << " + " << b << " = " << a + b << endl;
return 0 ;
}
4.2 编译和运行
编译 :按 Cmd+Shift+B 或通过终端菜单选择'运行生成任务'
调试 :按 F5 开始调试
直接运行 :在终端中执行 ./main
五、高级配置技巧
5.1 多文件项目配置 对于包含多个源文件的项目,修改 tasks.json:
{
"version" : "2.0.0" ,
"tasks" : [
{
"type" : "shell" ,
"label" : "build project" ,
"command" : "g++" ,
"args" : [ "-std=c++17" , "-g" , "src/*.cpp" , "-Iinclude" , "-o" , "bin/main" ] ,
"options" : { "cwd" : "${workspaceFolder}" } ,
"group" : { "kind" : "build" , "isDefault" : true } ,
"problemMatcher" : [ "$gcc" ]
}
]
}
5.2 使用 Makefile(推荐用于复杂项目) CXX = g++
CXXFLAGS = -std=c++17 -g -Wall
SRCDIR = src
INCDIR = include
SOURCES = $(wildcard $(SRCDIR) /*.cpp)
TARGET = bin/main
$(TARGET) : $(SOURCES)
$(CXX) $(CXXFLAGS) -I$(INCDIR) $^ -o $@
clean:
rm -f $(TARGET)
.PHONY : clean
{ "label" : "make build" , "type" : "shell" , "command" : "make" , "group" : "build" }
六、常见问题及解决方案
6.1 命令找不到错误 问题 :gcc: command not found
xcode-select --install
sudo ln -s /usr/local/bin/gcc-13 /usr/local/bin/gcc
sudo ln -s /usr/local/bin/g++-13 /usr/local/bin/g++
6.2 权限被拒绝 chmod +x /usr/local/bin/gcc*
6.3 头文件找不到 问题 :fatal error: iostream: No such file or directory
解决 :确保 c_cpp_properties.json 中的包含路径正确。
6.4 调试器无法工作
确保编译时加了 -g 参数
检查 launch.json 中的 MIMode 设置为 lldb(Mac 默认调试器)
七、实用技巧和优化建议
7.1 快捷键配置 在 keybindings.json 中添加自定义快捷键:
[ { "key" : "cmd+shift+b" , "command" : "workbench.action.tasks.build" } ]
7.2 代码格式化配置 安装 Clang-Format 扩展,并在设置中配置:
{ "C_Cpp.clang_format_style" : "{ BasedOnStyle: Google, IndentWidth: 4 }" }
7.3 推荐插件
Code Runner - 快速运行代码片段
Bracket Pair Colorizer - 括号彩色匹配
GitLens - Git 集成增强
结语 通过本文的详细步骤,你应该已经成功在 Mac 上配置好了 VSCode 的 C/C++ 开发环境。这个配置不仅支持基本的编译调试,还能够应对复杂的多文件项目开发。
✅ 代码智能提示正常工作
✅ 编译无错误(Cmd+Shift+B)
✅ 调试功能正常(F5)
✅ 多文件项目支持
微信扫一扫,关注极客日志 微信公众号「极客日志」,在微信中扫描左侧二维码关注。展示文案:极客日志 zeeklog
相关免费在线工具 Base64 字符串编码/解码 将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
Base64 文件转换器 将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online
Markdown 转 HTML 将 Markdown(GFM)转为 HTML 片段,浏览器内 marked 解析;与 HTML 转 Markdown 互为补充。 在线工具,Markdown 转 HTML在线工具,online
HTML 转 Markdown 将 HTML 片段转为 GitHub Flavored Markdown,支持标题、列表、链接、代码块与表格等;浏览器内处理,可链接预填。 在线工具,HTML 转 Markdown在线工具,online
JSON 压缩 通过删除不必要的空白来缩小和压缩JSON。 在线工具,JSON 压缩在线工具,online
JSON美化和格式化 将JSON字符串修饰为友好的可读格式。 在线工具,JSON美化和格式化在线工具,online