# CppEx20 **Repository Path**: jeffkuang_workspace/cpp-ex20 ## Basic Information - **Project Name**: CppEx20 - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-06-23 - **Last Updated**: 2026-07-07 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # C++20 练习题项目构建指南 ## 📋 项目结构 ``` C_C++/C++20Ex/ ├── cp01/ # 练习题目录 │ └── helloworld.cpp ├── cp02/ # 新的练习题目录... ├── bin/ # 编译输出目录 │ └── exercises/ # 练习可执行文件 │ └── cp01/ │ └── cp01_helloworld ├── build/ # CMake 构建目录 │ └── compile_commands.json ├── .vscode/ # VS Code 配置 ├── CMakeLists.txt # CMake 配置文件 └── compile_commands.json → build/compile_commands.json ``` ## 🚀 快速开始 ### 方式一:使用 Code Runner(推荐用于快速测试) 直接在 VS Code 中打开 `.cpp` 文件,按 `Ctrl+Alt+N` 或右键选择 "Run Code"。 **配置说明:** - ✅ 自动编译并运行当前文件 - ✅ 输出到 `bin/exercises/{子目录}/` 路径 - ✅ 在终端中显示结果 - ⚠️ 注意:Code Runner 生成的可执行文件名为 `{源文件名}`,而 CMake 生成的为 `{路径_源文件名}`(带路径前缀避免冲突) ### 方式二:使用 CMake 构建系统(推荐用于完整项目) #### 1. 首次配置 ```bash cmake -B build -DCMAKE_EXPORT_COMPILE_COMMANDS=ON . ``` #### 2. 构建所有题目 ```bash cmake --build build ``` #### 3. 运行程序 ```bash ./bin/exercises/cp01/cp01_helloworld ``` ### 方式三:使用 VS Code 任务 按 `Ctrl+Shift+B` 或 `F1` → "Tasks: Run Build Task" **可用任务:** - `CMake: Configure` - 配置项目 - `CMake: Build` - 构建所有目标(默认) - `CMake: Clean` - 清理构建目录 - `CMake: Reconfigure` - 清理并重新配置 ## ⚙️ VS Code 配置说明 ### c_cpp_properties.json - ✅ `cppStandard`: C++20 - ✅ `compileCommands`: 指向 compile_commands.json,提升 IntelliSense 准确性 ### settings.json - ✅ Code Runner 配置为使用 g++ -std=c++20 - ✅ 自动保存后运行 - ✅ 在终端中运行 ### tasks.json 提供 CMake 构建任务,方便一键配置和构建 ## 📝 添加新练习题 1. **创建新文件**(例如 `cp01/exercise2.cpp`) 2. **编写代码** 3. **选择以下任一方式:** **方式 A - Code Runner:** - 直接按 `Ctrl+Alt+N` 运行 **方式 B - CMake:** ```bash cmake --build build # 自动发现并编译新文件 ./bin/cp01/exercise2 # 运行 ``` ## 🔧 常见问题 ### Q: IntelliSense 报错怎么办? A: 确保已运行 `cmake -B build ...` 生成 compile_commands.json ### Q: 如何切换编译器? A: 修改 `CMakeLists.txt` 中的编译器设置,或在命令行使用: ```bash CXX=clang++ cmake -B build ... ``` ### Q: clangd 不工作? A: 检查以下几点: 1. compile_commands.json 是否存在 2. `.clangd` 配置是否正确 3. 重启语言服务器:`F1` → "Developer: Reload Window" ## 🎯 最佳实践 - ✅ 每个练习题独立编译,避免相互依赖 - ✅ 使用子目录组织不同章节的练习 - ✅ 定期清理 build 目录重新配置 - ✅ 使用 git 管理代码版本