0

I am exposing my C++ code to Python using pybind11. Let's say I'm doing exactly the example provided by the pybind developers. In the end, I'm building using

python setup.py build

The compiler is invoked with

clang ... -DNDEBUG ... -O3 ... -I... -std=c++14

My question is simple:

How can I, at least temporarily, switch on the assertions in my code? I.e. how can I tell the setuptools to omit the -DNDEBUG compiler option?

Tom de Geus
  • 5,625
  • 2
  • 33
  • 77

1 Answers1

2

Try

setup(…
    ext_modules=[Extension(…
        undef_macros=['DEBUG']
    …)]
…)

See https://docs.python.org/3/distutils/apiref.html#distutils.core.Extension

phd
  • 82,685
  • 13
  • 120
  • 165
  • Thanks a lot for your answer. This seems exactly what I need. Sadly though, it seems that it does not work for me... To verify that the option is actually invoked I also tried the illegal `undef_macros=['-DNDEBUG']` which raises indeed an error. Also `undef_macros=['DNDEBUG']` does not get rid of the define. – Tom de Geus Jun 07 '18 at 13:17
  • Shouldn't it be just `undef_macros=['NDEBUG']`? – phd Jun 07 '18 at 15:35
  • Sorry, then I did not make myself clear enough. I tried `undef_macros=['NDEBUG']` in addition to what I mention. With each of them the compiler option `-DNDEBUG` remains. – Tom de Geus Jun 07 '18 at 16:18
  • 1
    You should inspect your command line more carefully. It seems to work on my machine. With undef_macros=['NDEBUG'] the setup script emits `clang ... -DNDEBUG ... -UNDEBUG ...`. The -U effectively cancels prior define via -D. – Antonin Hildebrand Mar 31 '20 at 18:07