extra_compile_args in Cython
Asked Answered
F

3

28

I want to pass some extra options to the Cython compiler by using extra_compile_args.

My 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"]),
)

However, when I run python setup.py build_ext --inplace, I get the following warning:

UserWarning: got unknown compilation option, please remove: extra_compile_args

Question: How does one use extra_compile_args correctly?

I use Cython 0.23.4 under Ubuntu 14.04.3.

Filigree answered 4/11, 2015 at 11:25 Comment(0)
I
21

Use the more traditional way without cythonize to supply extra compiler options:

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}
)
Idyllic answered 4/11, 2015 at 12:24 Comment(1)
This approach doesn't seem to respect --inplace. See my workaround.Allochthonous
A
16

Mike Muller's answer works, but builds extensions in the current directory, not next to the .pyx file when --inplace is given as in:

python3 setup.py build_ext --inplace

So my workaround is to compose a CFLAGS string and override the env variable:

os.environ['CFLAGS'] = '-O3 -Wall -std=c++11 -I"some/custom/paths"'
setup(ext_modules = cythonize(src_list_pyx, language = 'c++'))
Allochthonous answered 13/10, 2016 at 21:24 Comment(0)
O
7

There's another way to do this, I found it to be the best one from other two presented because with this you still can use all regular cythonize arguments in usual way:

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++",
        ),
    ),
)
Orthogenesis answered 17/7, 2021 at 19:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.