How to specify C++11 with distutils?

你离开我真会死。 提交于 2020-07-17 04:33:12

问题


I have a module that needs to be compiled with C++11. On GCC and Clang, that means a std=c++11 switch, or std=c++0x on older compilers.

Python is not compiled with this switch so Distutils doesn't include it when compiling modules.

What is the preferred way to compile C++11 code with distutils?


回答1:


You can use the extra_compile_args parameter of distutils.core.Extension:

ext = Extension('foo', sources=[....],
                libraries=[....], 
                extra_compile_args=['-std=c++11'],
                ....)

Note that this is completely platform dependent. It won't even work on some older versions of gcc and clang.




回答2:


You can override the default values for various Distutils compilation and link flags using environment variables. This may require some experimentation depending on which platform you are on and how the Python you was using was built. But generally overriding CFLAGS will affect the compilation phase and either one of LDSHARED or LDFLAGS will affect the link phase.

export CFLAGS='-std=c++11'
pip install blah

or

export CFLAGS='-std=c++11'
python setup.py install

On OS X, another option is to use the ARCHFLAGS environment variable which has the advantage of not wiping out the original CFLAGS or LDSHARED values.



来源:https://stackoverflow.com/questions/23261320/how-to-specify-c11-with-distutils

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