VS Code C++ 环境配置及 HelloWorld 程序
一、环境配置(Windows/macOS/Linux)
1. 安装必要软件
Windows:
# 1. 安装 Visual Studio Code
# 下载地址:https://code.visualstudio.com/
# 2. 安装 MSVC 或 MinGW
# 方案A:安装 Visual Studio Build Tools
# 下载:https://visualstudio.microsoft.com/zh-hans/downloads/
# 勾选 "使用C++的桌面开发"
# 方案B:安装 MinGW-w64
# 下载:https://sourceforge.net/projects/mingw-w64/
macOS:
# 安装 Xcode Command Line Tools
xcode-select --install
# 或使用 Homebrew 安装 GCC
brew install gcc
Linux (Ubuntu/Debian):
sudo apt update
sudo apt install build-essential gdb
2. 安装 VS Code 扩展
打开 VS Code,安装以下扩展:
- C/C++ (Microsoft) - 代码补全、调试
- C/C++ Extension Pack - 扩展包(推荐)
- CMake Tools (可选) - CMake 支持
二、第一个 HelloWorld 程序
1. 创建项目目录结构
my-cpp-project/
├── .vscode/
│ ├── tasks.json # 编译任务配置
│ ├── launch.json # 调试配置
│ └── c_cpp_properties.json # 编译器配置
├── src/
│ └── main.cpp # 源代码
└── build/ # 编译输出目录(自动生成)
2. 编写 HelloWorld 程序
src/main.cpp:
#include <iostream>
int main() {
std::cout << "Hello, VS Code C++ World!" << std::endl;
// 一些额外的示例
std::cout << "================" << std::endl;
std::cout << "C++ 版本: " << __cplusplus << std::endl;
int a = 10, b = 20;
std::cout << "a + b = " << a + b << std::endl;
// 等待用户输入
std::cout << "\n按回车键退出...";
std::cin.get();
return 0;
}
三、VS Code 配置文件
1. .vscode/c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"${default}"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.19041.0",
"compilerPath": "C:/msys64/mingw64/bin/g++.exe", // 根据实际路径修改
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-gcc-x64"
}
],
"version": 4
}
2. .vscode/tasks.json - 编译任务
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe 构建活动文件",
"command": "g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${workspaceFolder}/src/main.cpp",
"-o",
"${workspaceFolder}/build/main.exe"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "编译器: g++.exe"
},
{
"label": "Clean Build",
"command": "rm",
"args": ["-rf", "${workspaceFolder}/build/*"],
"type": "shell",
"group": "build"
}
]
}
3. .vscode/launch.json - 调试配置
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) 启动",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/main.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:/msys64/mingw64/bin/gdb.exe", // 根据实际路径修改
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe 构建活动文件"
}
]
}
四、分平台配置方案
Windows 配置(MinGW)
// tasks.json (Windows MinGW)
{
"tasks": [
{
"label": "build",
"type": "shell",
"command": "g++",
"args": [
"-g",
"-std=c++17",
"${workspaceFolder}/src/*.cpp",
"-I${workspaceFolder}/include",
"-o",
"${workspaceFolder}/build/main.exe"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
macOS/Linux 配置
// tasks.json (macOS/Linux)
{
"tasks": [
{
"label": "build",
"type": "shell",
"command": "g++",
"args": [
"-g",
"-std=c++17",
"${workspaceFolder}/src/*.cpp",
"-I${workspaceFolder}/include",
"-o",
"${workspaceFolder}/build/main"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
五、编译和运行步骤
方法1:使用任务运行(推荐)
- Ctrl+Shift+B - 编译程序
- F5 - 编译并调试运行
- Ctrl+F5 - 运行程序(不调试)
方法2:使用终端
# 进入项目目录
cd my-cpp-project
# 创建build目录
mkdir -p build
# 编译
g++ -g -std=c++17 src/main.cpp -o build/main
# 运行 (Linux/macOS)
./build/main
# 运行 (Windows)
.\build\main.exe
六、调试功能使用
基础调试操作
- F9 - 设置/取消断点
- F5 - 开始调试
- F10 - 单步跳过
- F11 - 单步进入
- Shift+F5 - 停止调试
调试窗口
- 变量窗口 - 查看变量值
- 监视窗口 - 添加要监视的表达式
- 调用堆栈 - 查看函数调用关系
- 断点窗口 - 管理所有断点
七、使用 CMake(高级)
CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(HelloWorld)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(main src/main.cpp)
配置步骤
- 安装 CMake Tools 扩展
- 创建
CMakeLists.txt - 按 Ctrl+Shift+P,输入 "CMake: Configure"
- 按 F7 构建项目
八、常见问题解决
1. "g++ not found" 错误
# Windows: 确保 MinGW 已加入 PATH
# 检查方法:
g++ --version
# 在终端设置 PATH
set PATH=C:\mingw64\bin;%PATH%
2. 中文乱码问题
// 在 main.cpp 开头添加:
#ifdef _WIN32
#include <windows.h>
#endif
int main() {
#ifdef _WIN32
SetConsoleOutputCP(65001); // UTF-8
#endif
std::cout << "中文测试" << std::endl;
}
3. 调试不工作
- 检查
launch.json中的路径 - 确保编译时使用
-g参数 - 检查 GDB 是否正确安装
九、快速模板项目
可以从 GitHub 下载模板:
git clone https://github.com/yourusername/vscode-cpp-template.git
cd vscode-cpp-template
code .
现在你已经配置好了完整的 C++ 开发环境!开始编写代码吧!