如何在CMake释放模式下启用断言? [英] How to enable assert in CMake Release mode?

查看:419
本文介绍了如何在CMake释放模式下启用断言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CMake用于编译一些C ++文件。在代码中有 assert 调用。在CMake的释放模式下禁用这些呼叫。它在发布模式下定义 NDEBUG



如果我有兴趣在发布模式> > >

如果启用此功能,你只对自己的代码感兴趣 assert 功能,那么简单的一个解决方案
就是提供自定义断言。例如:

  #if(MY_DEBUG)
#define MY_ASSERT
#else
#define MY_ASSERT(A)...忽略A ...
#endif

使用选项启用/禁用assert:

 #CMakeLists.txt 
选项(ENABLE_MY_ASSERT打开MY_ASSERT检查关闭)
if(ENABLE_MY_ASSERT)
add_definitions(-DMY_DEBUG = 1)
else b $ b add_definitions(-DMY_DEBUG = 0)
endif()

可以完全控制您的支票,您可以验证一个
组件并忽略其他:

  ... FOO_DEBUG = 0 BOO_DEBUG = 1 BAR_DEBUG = 0 ... 



2


$ b b

添加自定义 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)

输出:

 #调试
#... -g ...

#发布
# ... -O3 -DNDEBUG ...

#RelWithDebInfo
#... -O2 -g -DNDEBUG ...

#MyRel
#... -O3 ...


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.

If I'm interested in having assert in Release mode of CMake, how do I enable it?

解决方案

1

If you interested in assert functionality only in your own code then the simple one solution is to provide custom assert. For instance:

#if (MY_DEBUG)
# define MY_ASSERT(A) ... checks here ...
#else
# define MY_ASSERT(A) ... ignore A ...
#endif

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 one component and ignore others:

... FOO_DEBUG=0 BOO_DEBUG=1 BAR_DEBUG=0 ...

2

Add custom CMAKE_BUILD_TYPE (also see CMAKE_CONFIGURATION_TYPES):

cmake_minimum_required(VERSION 2.8.12)
project(foo)

set(CMAKE_CXX_FLAGS_MYREL "-O3")

add_library(foo foo.cpp)

output:

# Debug
# ... -g ...

# Release
# ... -O3 -DNDEBUG ...

# RelWithDebInfo
# ... -O2 -g -DNDEBUG ...

# MyRel
# ... -O3 ...

这篇关于如何在CMake释放模式下启用断言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆