本次更新为 PHPX 项目添加了完整的调试支持,方便在开发和排查问题时使用。
- ✅ Windows 平台:
/Zi(调试符号),/Od(禁用优化),/RTC1(运行时检查) - ✅ Linux/macOS 平台:
-g(调试符号),-O0(禁用优化),-DDEBUG=1 - ✅ 自动生成 PDB 文件 (Windows) 或 DWARF 信息 (Linux/macOS)
- ✅ Windows Debug 模式下启用
/showIncludes显示头文件包含信息 - ✅ 配置摘要中显示详细的调试模式信息
-
debugging-guide.md - 完整的调试指南
- 如何启用 Debug 模式
- 各平台调试工具使用说明
- 常见问题排查方法
- 调试技巧和最佳实践
-
debugging-quick-reference.md - 调试快速参考
- 常用调试命令速查
- GDB/Valgrind/ASAN 使用方法
- 问题诊断流程
-
README.md - 添加 Debug 模式说明
- 快速开始部分增加 Debug 构建说明
- Debug 模式特性列表
-
testing-guide.md - 更新测试指南
- 添加 Debug 模式编译说明
- Debug 模式优势说明
-
docs/README.md - 更新文档索引
- 添加调试相关文档链接
- examples/test/debug_example.cpp - 调试功能示例
- 条件编译调试输出
- 断言使用
- 资源跟踪
- 性能计时
- 调试宏定义
交互式批处理脚本,支持:
- Debug 模式构建
- Release 模式构建
- RelWithDebInfo 模式构建
- 清理构建文件
交互式 shell 脚本,支持:
- Debug 模式构建
- Release 模式构建
- RelWithDebInfo 模式构建
- AddressSanitizer 构建
- 清理构建文件
Windows:
.\debug-build.bat
# 选择选项 1 (Debug)Linux/macOS:
chmod +x debug-build.sh
./debug-build.sh
# 选择选项 1 (Debug)Windows:
cmake -DCMAKE_BUILD_TYPE=Debug .
cmake --build . --config DebugLinux/macOS:
cmake -DCMAKE_BUILD_TYPE=Debug .
make -j 4Visual Studio:
cmake -G "Visual Studio 16 2019" -DCMAKE_BUILD_TYPE=Debug .
# 打开生成的 .sln 文件
# 选择 Debug 配置
# 按 F5 调试- ✅ 完整的调试符号(支持断点调试和堆栈跟踪)
- ✅ 禁用编译器优化(变量值可见,代码执行顺序一致)
- ✅ 详细的编译输出信息
- ✅ 运行时错误检查 (
/RTC1)- 检测未初始化的变量
- 检测栈帧损坏
- 检测整数溢出
- ✅ 显示头文件包含信息 (
/showIncludes) - ✅ 生成 PDB 文件(用于 Visual Studio 调试)
- ✅ GCC/GDB 完整调试支持
- ✅ Valgrind 内存检查支持
- ✅ AddressSanitizer 支持
- ✅ 定义 DEBUG 宏(可用于条件编译)
gdb ./bin/phpx-tests
(gdb) run
(gdb) bt # 查看堆栈
(gdb) print var # 查看变量- 打开 .sln 文件
- 设置断点
- 按 F5 调试
- 查看调用堆栈和变量
valgrind --leak-check=full ./bin/phpx-testscmake -Denable_asan=ON -DCMAKE_BUILD_TYPE=Debug .
make
./bin/phpx-tests # ASAN 自动检测内存错误# 1. 使用 Debug 模式重新编译
cmake -DCMAKE_BUILD_TYPE=Debug .
make
# 2. 用 GDB 定位
gdb ./bin/phpx-tests
(gdb) run
(gdb) bt# 使用 Valgrind
valgrind --leak-check=full --show-leak-kinds=all ./bin/phpx-tests# 启用 UBSAN
cmake -DCMAKE_CXX_FLAGS="-fsanitize=undefined" .
make
./bin/phpx-tests#ifdef DEBUG
std::cerr << "[DEBUG] Variable value: " << x << std::endl;
#endif#include <cassert>
assert(pointer != nullptr);
assert(size > 0);#define DEBUG_LOG(msg) \
do { \
#ifdef DEBUG \
std::cerr << "[DEBUG] " << __FILE__ << ":" << __LINE__ \
<< ": " << msg << std::endl; \
#endif \
} while(0)- Debug 模式: 适合开发和调试,但性能较低
- Release 模式: 适合生产环境,性能最优
- RelWithDebInfo: 平衡性能和调试能力
建议在开发过程中使用 Debug 模式,发布前切换到 Release 模式。
CMakeLists.txt- 构建配置debug-build.bat- Windows 构建脚本debug-build.sh- Linux/macOS 构建脚本docs/debugging-guide.md- 详细调试指南docs/debugging-quick-reference.md- 快速参考examples/test/debug_example.cpp- 调试示例代码
- 尝试使用 Debug 模式编译项目
- 阅读调试指南了解更多信息
- 在代码中添加调试输出和断言
- 使用调试工具排查问题
文档创建时间:2026-04-30