跳到主要内容

C++ 开发环境配置

在开始 C++ 编程之前,我们需要配置好开发环境。本章将介绍在不同操作系统上配置 C++ 开发环境的方法。

常见的 C++ 编译器

1. GCC (GNU Compiler Collection)

  • 平台: Linux, macOS, Windows (MinGW)
  • 特点: 开源免费,跨平台,支持最新 C++ 标准
  • 命令: g++

2. Clang

  • 平台: macOS (默认), Linux, Windows
  • 特点: 编译速度快,诊断信息清晰
  • 命令: clang++

3. MSVC (Microsoft Visual C++)

  • 平台: Windows (Visual Studio)
  • 特点: Windows 平台最完整的支持
  • 命令: cl.exe

4. ICC (Intel C++ Compiler)

  • 平台: Linux, Windows
  • 特点: 针对 Intel 处理器优化

Windows 环境配置

方法一:使用 Visual Studio(推荐)

Visual Studio 是 Windows 上最完整的 C++ 开发环境。

下载安装步骤:

  1. 访问 Visual Studio 官网
  2. 下载 Community 版本(免费)
  3. 运行安装程序,选择「使用 C++ 的桌面开发」工作负载
┌─────────────────────────────────────────┐
│ Visual Studio 安装向导 │
├─────────────────────────────────────────┤
│ □ .NET 桌面开发 │
│ ✓ 使用 C++ 的桌面开发 ← 勾选这个 │
│ □ ASP.NET 和 Web 开发 │
│ □ Python 开发 │
│ □ 数据科学和分析应用程序 │
└─────────────────────────────────────────┘

创建第一个项目:

  1. 打开 Visual Studio
  2. 选择「创建新项目」
  3. 选择「空项目」
  4. 输入项目名称和位置
  5. 添加新的 C++ 源文件:右键 → 添加 → 新建项 → C++ 文件

编译运行:

  • Ctrl + F5 编译并运行
  • 或点击顶部菜单「调试 → 开始执行」

方法二:使用 VS Code + MinGW

如果更喜欢轻量级的编辑器,可以使用 VS Code 配合 MinGW。

步骤 1:下载 MinGW

  1. 下载 MinGW-w64 或使用 MSYS2
  2. 安装后确保 bin 目录在系统 PATH 中

步骤 2:安装 VS Code

  1. 下载 VS Code
  2. 安装以下扩展:
    • C/C++ (Microsoft)
    • C/C++ Extensions (ms-vscode.cpptools)

步骤 3:配置 VS Code

创建 .vscode/tasks.json:

{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: g++ build",
"command": "g++",
"args": [
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.exe",
"-g",
"-std=c++17"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}

创建 .vscode/launch.json:

{
"version": "0.2.0",
"configurations": [
{
"name": "C++ Launch",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "gdb"
}
]
}

Linux 环境配置

Ubuntu/Debian

# 安装 GCC 编译器
sudo apt update
sudo apt install g++

# 验证安装
g++ --version

CentOS/RHEL

# 安装 GCC 编译器
sudo yum install gcc-c++

# 或使用 DNF (CentOS 8+)
sudo dnf install gcc-c++

使用 VS Code(可选)

# 安装 VS Code
sudo apt install code

# 或从官网下载 .deb 包安装

安装 C/C++ 扩展后,VS Code 会自动检测系统中的编译器。

macOS 环境配置

使用命令行工具

macOS 通常已经预装了 Clang:

# 打开终端,运行以下命令安装命令行工具
xcode-select --install

# 验证安装
clang++ --version

使用 Homebrew 安装 GCC

# 安装 Homebrew(如果没有)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# 安装 GCC
brew install gcc

# 查看版本
g++-13 --version # 版本号可能不同

使用 Xcode

  1. 从 App Store 下载 Xcode
  2. 打开 Xcode,选择「Create a new project」
  3. 选择「Command Line Tool」作为模板

验证开发环境

创建一个测试文件 hello.cpp

#include <iostream>

int main() {
std::cout << "Hello, C++!" << std::endl;
return 0;
}

使用命令行编译

Linux/macOS:

# 编译
g++ hello.cpp -o hello

# 或使用 clang++
clang++ hello.cpp -o hello

# 运行
./hello

Windows (MinGW):

g++ hello.cpp -o hello.exe
hello.exe

Windows (Visual Studio):

打开 Developer Command Prompt:

cl /EHsc hello.cpp
hello.exe

预期输出

Hello, C++!

IDE 对比

IDE平台优点缺点
Visual StudioWindows功能完整,调试强大较重,仅支持 Windows
XcodemacOSmacOS 原生,调试方便仅支持 macOS
VS Code跨平台轻量,可定制需要额外配置
CLion跨平台专业,智能提示收费
Eclipse跨平台免费,可扩展较老旧

编译选项说明

常用的 g++ 编译选项:

选项说明
-o <file>指定输出文件名
-std=c++17使用 C++17 标准
-Wall启用所有警告
-g生成调试信息
-O2优化级别 2
-I<dir>添加头文件搜索路径
-L<dir>添加库文件搜索路径
-l<library>链接库文件

示例

# 启用 C++20,生成调试信息,启用所有警告
g++ -std=c++20 -g -Wall -O2 main.cpp -o main

# 链接数学库
g++ main.cpp -o main -lm

常见问题

Q: g++ 命令找不到?

解决方法:

  • Windows: 安装 MinGW 并添加到 PATH
  • Linux: sudo apt install g++
  • macOS: xcode-select --install

Q: 编译报错 "undefined reference to..."?

可能原因:

  • 没有链接需要的库(如 -lm 链接数学库)
  • 函数实现未包含在代码中

Q: 中文乱码?

解决方法:

  • 保存源文件时选择 UTF-8 编码
  • Windows 下可以使用 setlocale(LC_ALL, "");

下一步

环境配置完成后,让我们开始学习 C++ 基础语法

建议

对于初学者,Windows 建议使用 Visual Studio,macOS 建议使用 Xcode,Linux 建议使用 VS Code + GCC。这些组合可以帮助你快速上手,避免环境配置的困扰。