11

I'm wrapping a C++ class with Python, and I cannot compile any C++11 features with the Cython module.

Everything compiles okay when compiling the C++ alone. But when I run this setup.py below:

setup(
    ext_modules = cythonize(
       "marketdata.pyx",            # our Cython source
       sources=["cpp/OBwrapper.cpp, cpp/OrderBook/orderbook.h, cpp/OrderBook/orderbook.cpp"],  # additional source file(s)
       language="c++",             # generate C++ code
       extra_compile_args=["-std=c++11"]
    ))

In my .pyx file header:

# distutils: language = c++
# distutils: sources = cpp/OBwrapper.cpp cpp/OrderBook/orderbook.cpp

I get a ton of errors that have to do with them not recognizing c++11 commands, like 'auto'.

For example:

cpp/OrderBook/orderbook.cpp(168) : error C2065: 'nullptr' : undeclared identifier

How can I get this to work?

The Dude
  • 330
  • 6
  • 16
  • 5
    I'm thinking that's a Microsoft-ish error message, and `-std=c++11` is a GNU-ish compile option. – Fred Larson Dec 04 '14 at 22:44
  • @FredLarson: I don't think so, I'm getting this error on Linux. `cythonize` simply ignores `extra_compile_args`. See also [this answer](http://stackoverflow.com/a/33521863/1804173). – bluenote10 Sep 19 '16 at 20:21
  • @bluenote10: [`error C2065`](https://msdn.microsoft.com/en-us/library/ewcf0002.aspx) is a Visual Studio error message. I'd be surprised to see it on Linux. – Fred Larson Sep 19 '16 at 21:18
  • @FredLarson: Of course, the error is different, but it is the same cause: The compiler is called without `-std=c++11`. I though you are saying that `cythonize` in fact _does_ pass `extra_compile_args` to the compiler and the error is caused by a specific compiler... – bluenote10 Sep 19 '16 at 21:28
  • @bluenote10: I was just noting the apparent inconsistency between the error message and the compiler switch. `-std=c++11` would apply to GNU, and it appears to me that MSVC didn't even have a standards version switch [until Visual C++ 2015 Update 3](https://blogs.msdn.microsoft.com/vcblog/2016/06/07/standards-version-switches-in-the-compiler/). I don't know a thing about `cythonize`. – Fred Larson Sep 19 '16 at 21:32

1 Answers1

7

Try using Extension: setup(ext_modules=cythonize([Extension(...)], ...).

This setup.py works for me (on Debian Linux):

from setuptools import setup, find_packages, Extension
from Cython.Build import cythonize
from glob import glob

extensions = [
    Extension(
        'my_proj.cython.hello',
        glob('my_proj/cython/*.pyx')
        + glob('my_proj/cython/*.cxx'),
        extra_compile_args=["-std=c++14"])
]

setup(
    name='my-proj',
    packages=find_packages(exclude=['doc', 'tests']),
    ext_modules=cythonize(extensions))
Messa
  • 24,321
  • 6
  • 68
  • 92