问题 Cython中的extra_compile_args


我想传递一些额外的选项 Cython 使用编译器 extra_compile_args

我的 setup.py

from distutils.core import setup

from Cython.Build import cythonize

setup(
  name = 'Test app',
  ext_modules = cythonize("test.pyx", language="c++", extra_compile_args=["-O3"]),
)

但是,当我跑 python setup.py build_ext --inplace,我得到以下警告:

UserWarning: got unknown compilation option, please remove: extra_compile_args

题: 如何使用 extra_compile_args 是否正确?

我用 Cython 0.23.4 下 Ubuntu 14.04.3


10707
2017-11-04 11:25


起源



答案:


没有使用更传统的方式 cythonize 提供额外的编译器选项:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

setup(
  name = 'Test app',
  ext_modules=[
    Extension('test',
              sources=['test.pyx'],
              extra_compile_args=['-O3'],
              language='c++')
    ],
  cmdclass = {'build_ext': build_ext}
)

11
2017-11-04 12:24



这种方法似乎并不尊重 --inplace。看看我的解决方法。 - Nick


答案:


没有使用更传统的方式 cythonize 提供额外的编译器选项:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

setup(
  name = 'Test app',
  ext_modules=[
    Extension('test',
              sources=['test.pyx'],
              extra_compile_args=['-O3'],
              language='c++')
    ],
  cmdclass = {'build_ext': build_ext}
)

11
2017-11-04 12:24



这种方法似乎并不尊重 --inplace。看看我的解决方法。 - Nick


Mike Muller的答案有效,但在当前目录中构建扩展,而不是在 .pyx 档案时 --inplace 给出如下:

python3 setup.py build_ext --inplace

所以我的解决方法是编写CFLAGS字符串并覆盖env变量:

os.environ['CFLAGS'] = '-O3 -Wall -std=c++11 -I"some/custom/paths"'
setup(ext_modules = cythonize(src_list_pyx, language = 'c++'))

4
2017-10-13 21:24