AgentLint 实测验证 89/100基于 95 个 C++ 开源仓库统计
C++ CLAUDE.md & AGENTS.md 模板
CMake/Ninja 预设、clang-format、AddressSanitizer 与 RAII 内存安全,提炼自 C++ 开源仓库。
# C++ Project — C++20 Harness C++20 project built with CMake presets + Ninja and verified with GoogleTest and AddressSanitizer. ## Commands - Configure: `cmake --preset debug` - Incremental build: `cmake --build --preset debug -j` - Fast single test: `ctest --preset debug -R TargetTest --output-on-failure` - Format check: `clang-format --dry-run --Werror src/*.cpp include/*.hpp` ## Rules - Don't invoke raw `g++` or `clang++` directly. Instead build through CMake presets. Because direct compilation bypasses project defines and sanitizer flags. - Don't use raw owning `new`/`delete`. Instead use RAII (`std::unique_ptr`, `std::vector`, `std::span`). Because manual ownership leaks under early returns. - When modifying public headers or ABI structs, read `docs/abi-policy.md`. ## Continuity - Keep `build/` and `CLAUDE.local.md` gitignored and record benchmark deltas in `HANDOFF.md`.
C++ 开源生态中最常采用的工程规则
基于 4,533 仓库研究中 95 个公开 C++ 仓库的实测统计:
cmake工具链
CMake build system
52.7%29 个仓库
modern-cpp代码规范
Modern C++ (C++17/20/23)
45.5%25 个仓库
stl代码规范
Use STL algorithms and containers
34.5%19 个仓库
unit-tests测试
Write unit tests for all changes
32.7%18 个仓库
smart-pointers代码规范
Use smart pointers (unique_ptr, shared_ptr)
25.5%14 个仓库
namespaces代码规范
Use namespaces appropriately
23.6%13 个仓库
error-handling代码规范
Proper error handling with context
20.0%11 个仓库
ci-cd工具链
CI/CD pipeline must pass
20.0%11 个仓库
必写验证命令(对应规则 W1 与 W7)
- 安装依赖
- cmake --preset debug
- 快速单测(秒级迭代)
- ctest --preset debug -R TargetTest --output-on-failure
- 全量测试
- ctest --preset debug --output-on-failure
- Lint 与静态检查
- clang-format --dry-run --Werror src/*.cpp include/*.hpp
- 生产构建
- cmake --build --preset debug -j
C++ 项目中 AI 代理常见踩坑
- 01AI 直接拼原生
g++命令跳过 CMake 预设,漏掉头文件路径和 Sanitizer 编译开关。 - 02手写
new/delete代替std::unique_ptr极易在提前返回路径造成内存泄漏。