How May I Override the Compiler (Gcc) Flags That Setup.Py Uses by Default

How may I override the compiler (GCC) flags that setup.py uses by default?

  • Prepend CFLAGS="-O0" before you run setup.py:

    % CFLAGS="-O0" python ./setup.py

    The -O0 will be appended to CFLAGS while compiling, therefore will override previous -O2 setting.

  • Another way is add -O0 to extra_compile_args in setup.py:

    moduleA = Extension('moduleA', .....,
    include_dirs = ['/usr/include', '/usr/local/include'],
    extra_compile_args = ["-O0"],
    )
  • If you want to remove all default flags, use:

    % OPT="" python ./setup.py

How does one overwrite the default compile flags for Cython when building with distutils?

larsmans comment was right - using /usr/bin/cython addresses my issue.

Distutils compiler options configuration

I actually export them to the environment, just like for autotools' configure:

export CC=/usr/local/bin/clang
export CFLAGS=-I${HOME}/include
export LDFLAGS=-lboost

If you also need to override the linker separately:

export LDSHARED=/usr/local/bin/clang -shared

And if you don't like exporting the settings to your environment, do something like this for a one-time setting:

CC=/usr/local/bin/clang CFLAGS=-I${HOME}/include python setup.py build

If you want to find out what the default options were when python was build, use python-config --<flag>. Some flags are cflags, ldflags, libs or includes.

How to override -DNDEBUG compile flag when building a Cython module

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

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.

Possible to change distutils default compiler options?

The option is in distutils/cygwinccompiler.py. If you can edit the file in the Python installation ­just remove it.



Related Topics



Leave a reply



Submit