构建cython模块时如何覆盖-DNDEBUG编译标志 [英] how to override -DNDEBUG compile flag when building cython module

查看:99
本文介绍了构建cython模块时如何覆盖-DNDEBUG编译标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Cython模块,该模块通过 cdef extern 调用C ++函数。 C ++函数具有 assert()语句,我想检查这些断言。但是,当我通过调用 python setup.py build_ext --inplace 创建模块时,总是使用 -DNDEBUG 。只要运行代码,就不会检查断言。

I have a Cython module that calls a C++ function via cdef extern. The C++ function has assert() statements, and I would like to check those assertions. However, when I create the module by calling python setup.py build_ext --inplace, gcc is always invoked with -DNDEBUG. Whenever the code is run, the assertions are not checked.

我找不到覆盖 -DNDEBUG 使用 setup.py 。这可能吗?

I can't find a way to override -DNDEBUG using setup.py. Is this possible?

目前,我发现处理此问题的唯一方法是使用 python使用的选项手动调用cython,gcc和g ++ setup.py ,但取出 -DNDEBUG 。但是必须有一种更简单的方法。

Currently the only way I have found to deal with this is to manually call cython, gcc, and g++ with the options that are used by python setup.py, but to take out -DNDEBUG. But there must be a simpler way.

推荐答案

您可以手动取消定义 NDEBUG (如果已定义),则在包含< cassert> 之前。将以下行添加到包含这些assert语句的cpp文件的顶部。确保这些是该文件中的第一条语句。

You can manually undefine NDEBUG, if it is defined, prior to including <cassert>. Add the following lines to the top of the cpp file which contains these assert statements. Make sure these are the very first statements in that file.

#ifdef NDEBUG
# define NDEBUG_DISABLED
# undef NDEBUG
#endif
#include <cassert>

#ifdef NDEBUG_DISABLED
# define NDEBUG        // re-enable NDEBUG if it was originally enabled
#endif

// rest of the file

这将确保在以下情况下未定义 NDEBUG 处理器包含< cassert> ,这将导致断言检查被编译到您的代码中。

This will ensure that NDEBUG is not defined when the processor includes <cassert>, which will result in the assertion checks being compiled into your code.

这篇关于构建cython模块时如何覆盖-DNDEBUG编译标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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