extra_compile_args 在 Cython 中

extra_compile_args in Cython

我想通过使用 extra_compile_args.

将一些额外的选项传递给 Cython 编译器

我的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

我在Ubuntu 14.04.3下使用Cython 0.23.4

使用没有 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}
)

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

python3 setup.py build_ext --inplace

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

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

还有另一种方法可以做到这一点,我发现它是其他两种方法中最好的一种,因为有了它,您仍然可以按照通常的方式使用所有常规 cythonize 参数:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize

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