C++安装 #

一、C++开发环境概述 #

C++开发环境主要包括两部分:

  1. 编译器:将源代码编译成可执行程序
  2. 编辑器/IDE:编写代码的工具

1.1 常用编译器 #

编译器 平台 特点
GCC 跨平台 开源免费,标准支持好
Clang 跨平台 编译速度快,错误提示友好
MSVC Windows Visual Studio自带,Windows优化好

1.2 常用IDE/编辑器 #

工具 平台 特点
Visual Studio Windows 功能强大,调试方便
CLion 跨平台 JetBrains出品,智能提示好
VS Code 跨平台 轻量级,插件丰富
Xcode macOS 苹果官方IDE

二、Windows安装 #

2.1 安装Visual Studio(推荐) #

  1. 下载Visual Studio Community(免费版)

  2. 安装时选择"使用C++的桌面开发"

  3. 验证安装

    • 打开Visual Studio
    • 创建新项目 → 空项目
    • 添加源文件并编译运行

2.2 安装MinGW(轻量级) #

  1. 下载MinGW-w64

  2. 使用MSYS2安装(推荐):

bash
# 安装MSYS2后,在MSYS2终端执行:
pacman -Syu
pacman -S mingw-w64-x86_64-gcc
pacman -S mingw-w64-x86_64-gdb
  1. 配置环境变量

    • C:\msys64\mingw64\bin 添加到PATH
  2. 验证安装:

bash
g++ --version
gdb --version

2.3 VS Code配置 #

  1. 安装VS Code

  2. 安装扩展:

    • C/C++(Microsoft)
    • C/C++ Extension Pack
    • Code Runner
  3. 配置 tasks.json

json
{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ build",
            "command": "g++",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}.exe"
            ],
            "problemMatcher": ["$gcc"],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

三、macOS安装 #

3.1 安装Xcode命令行工具 #

bash
# 安装命令行工具
xcode-select --install

这会安装Clang编译器。

3.2 安装Homebrew #

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

3.3 安装GCC(可选) #

bash
# 使用Homebrew安装GCC
brew install gcc

# 验证安装
g++-13 --version

3.4 安装CMake #

bash
brew install cmake
cmake --version

3.5 VS Code配置 #

  1. 安装VS Code
  2. 安装C/C++扩展
  3. 配置 c_cpp_properties.json
json
{
    "configurations": [
        {
            "name": "macOS-Clang",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "compilerPath": "/usr/bin/clang++",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "macos-clang-arm64"
        }
    ],
    "version": 4
}

四、Linux安装 #

4.1 Ubuntu/Debian #

bash
# 更新包列表
sudo apt update

# 安装编译工具链
sudo apt install build-essential

# 安装GDB调试器
sudo apt install gdb

# 安装CMake
sudo apt install cmake

# 验证安装
g++ --version
gdb --version
cmake --version

4.2 CentOS/RHEL #

bash
# 安装开发工具组
sudo yum groupinstall "Development Tools"

# 或使用dnf(较新版本)
sudo dnf groupinstall "Development Tools"

# 安装CMake
sudo yum install cmake
# 或
sudo dnf install cmake

# 验证安装
g++ --version

4.3 Arch Linux #

bash
# 安装基础开发工具
sudo pacman -S base-devel

# 安装GCC
sudo pacman -S gcc

# 安装CMake
sudo pacman -S cmake

# 验证安装
g++ --version

五、编译与运行 #

5.1 命令行编译 #

创建 hello.cpp 文件:

cpp
#include <iostream>

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

编译并运行:

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

# 运行
./hello        # Linux/macOS
hello.exe      # Windows

5.2 常用编译选项 #

bash
# 指定C++标准
g++ -std=c++17 hello.cpp -o hello

# 开启警告
g++ -Wall hello.cpp -o hello

# 生成调试信息
g++ -g hello.cpp -o hello

# 优化级别
g++ -O2 hello.cpp -o hello

# 组合使用
g++ -std=c++17 -Wall -g -O2 hello.cpp -o hello

5.3 使用CMake构建 #

创建 CMakeLists.txt

cmake
cmake_minimum_required(VERSION 3.10)
project(HelloWorld)

set(CMAKE_CXX_STANDARD 17)

add_executable(hello hello.cpp)

构建步骤:

bash
# 创建构建目录
mkdir build
cd build

# 生成构建文件
cmake ..

# 编译
cmake --build .

# 运行
./hello

六、IDE配置详解 #

6.1 Visual Studio #

  1. 创建项目

    • 文件 → 新建 → 项目
    • 选择"空项目"或"控制台应用"
  2. 配置项目

    • 右键项目 → 属性
    • C/C++ → 语言 → C++语言标准
  3. 调试设置

    • 设置断点(F9)
    • 开始调试(F5)

6.2 CLion #

  1. 创建项目

    • New Project
    • 选择C++ Executable
  2. 配置工具链

    • Settings → Build → Toolchains
    • 自动检测编译器
  3. 运行调试

    • 点击运行按钮或Shift+F10
    • 点击调试按钮或Shift+F9

6.3 VS Code #

  1. 创建 launch.json
json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++ debug",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "/usr/bin/gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++ build"
        }
    ]
}

七、验证开发环境 #

7.1 编译器版本检查 #

bash
# GCC
g++ --version

# Clang
clang++ --version

# MSVC (在Visual Studio开发者命令提示符中)
cl

7.2 C++标准支持检查 #

创建 check_version.cpp

cpp
#include <iostream>

int main() {
    std::cout << "C++ Standard: ";
    
    #if __cplusplus == 202302L
        std::cout << "C++23" << std::endl;
    #elif __cplusplus == 202002L
        std::cout << "C++20" << std::endl;
    #elif __cplusplus == 201703L
        std::cout << "C++17" << std::endl;
    #elif __cplusplus == 201402L
        std::cout << "C++14" << std::endl;
    #elif __cplusplus == 201103L
        std::cout << "C++11" << std::endl;
    #else
        std::cout << "C++98/C++03" << std::endl;
    #endif
    
    return 0;
}

编译运行:

bash
g++ -std=c++17 check_version.cpp -o check_version
./check_version

八、总结 #

搭建C++开发环境的步骤:

平台 推荐方案
Windows Visual Studio 或 MinGW + VS Code
macOS Xcode Command Line Tools + VS Code/CLion
Linux GCC/G++ + VS Code/CLion

关键要点:

  • 选择适合的编译器(GCC、Clang、MSVC)
  • 配置好调试环境
  • 了解常用编译选项
  • 掌握CMake等构建工具

下一步,让我们编写第一个C++程序!

最后更新:2026-03-26