【发布时间】:2022-01-17 16:48:42
【问题描述】:
我希望以下代码仅在调试模式下编译
main.cpp
#ifdef __DEBUG__
int a=1;
std::cout<<a;
#endif
在 cmake 中添加以下内容
add_compile_options(
"-D__DEBUG__"
)
或
add_compile_options(
"$<$<CONFIG:DEBUG>:-D__DEBUG__>"
)
似乎什么也没做。
我怎样才能实现期望的行为?
【问题讨论】:
-
你重建了makefile吗?
-
预定义的调试配置被命名为
Debug那么add_compile_options( "$<$<CONFIG:Debug>:-D__DEBUG__>")工作吗?还有一个add_compile_definitions命令可以在没有-D前缀的情况下使用:add_compile_definitions($<$<CONFIG:Debug>:__DEBUG__>) -
add_compile_definitions($<$<CONFIG:Debug>:__DEBUG__>)与#if defined(__DEBUG__)结合使用。谢谢:) -
不要在 CMake 中使用和推荐目录命令!!它们会带你走上绝对疯狂的道路。使用
target_compile_definitions而不是add_compile_definitions。