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

ⅰ亾dé卋堺 提交于 2019-11-26 08:06:21

问题


I understand that setup.py uses the same CFLAGS that were used to build python. I have a single C extension of ours that is segfaulting. I need to build it without -O2 because -O2 is optimizing out some values and code so that the core files are not sufficient to pin down the problem.

I just need to modify setup.py so that -O2 is not used.

I\'ve read distutils documentation, in particular distutils.ccompiler and distutils.unixcompiler and see how to add flags and libs and includes, but not how to modify the default gcc flags.

Specifically, this is for a legacy product on Python 2.5.1 with a bunch of backports (Fedora 8, yes, I know...). No, I cannot change the OS or python version and I cannot, without great problems, recompile python. I just need to build a one off of the C extension for one customer whose environment is the only one segfaulting.


回答1:


  • 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
    


来源:https://stackoverflow.com/questions/6928110/how-may-i-override-the-compiler-gcc-flags-that-setup-py-uses-by-default

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!