问题描述
CMake 被用来编译一些 C++ 文件.代码中有 assert
调用.这些调用在 CMake 的 Release 模式下被禁用.我猜它在发布模式下定义了 NDEBUG
.
CMake is being used to compile some C++ files. There are assert
calls in the code. These calls are disabled in Release mode of CMake. It defines NDEBUG
in Release mode, I guess.
如果我有兴趣在 CMake 的 Release 模式下使用断言,我该如何启用它?
If I'm interested in having assert in Release mode of CMake, how do I enable it?
推荐答案
1
如果您只对自己代码中的 assert
功能感兴趣,那么简单的一种解决方案是提供自定义断言.例如:
1
If you interested in assert
functionality only in your own code then the simple one solutionis to provide custom assert. For instance:
#if (MY_DEBUG)
# define MY_ASSERT(A) ... checks here ...
#else
# define MY_ASSERT(A) ... ignore A ...
#endif
使用 option
启用/禁用断言:
Use option
to enable/disable assert:
# CMakeLists.txt
option(ENABLE_MY_ASSERT "Turn on MY_ASSERT checks" OFF)
if(ENABLE_MY_ASSERT)
add_definitions(-DMY_DEBUG=1)
else()
add_definitions(-DMY_DEBUG=0)
endif()
在这种情况下,您可以完全控制您的支票,您可以验证一个组件并忽略其他组件:
In this case you have full control over your checks, you can verify onecomponent and ignore others:
... FOO_DEBUG=0 BOO_DEBUG=1 BAR_DEBUG=0 ...
2
添加自定义CMAKE_BUILD_TYPE(另见CMAKE_CONFIGURATION_TYPES):
cmake_minimum_required(VERSION 2.8.12)
project(foo)
set(CMAKE_CXX_FLAGS_MYREL "-O3")
add_library(foo foo.cpp)
输出:
# Debug
# ... -g ...
# Release
# ... -O3 -DNDEBUG ...
# RelWithDebInfo
# ... -O2 -g -DNDEBUG ...
# MyRel
# ... -O3 ...
这篇关于如何在 CMake Release 模式下启用断言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!