Tom Swirly Tom Swirly - 7 months ago 54
Python Question

Cython build can't find C++11 STL files - but only when called from setup.py

When I compile my code from setup.py, it can't find the C++11 include file

<array>
- but C++11 compiler features do work.

When I paste the same command line that setup.py generates into my shell, it all compiles perfectly well(!)

Code demonstrating this behavior can be seen here and is also pasted below.




Terminal session:

$ python setup.py build_ext
running build_ext
building 'simple' extension
creating build
creating build/temp.macosx-10.6-intel-3.4
/usr/bin/clang -fno-strict-aliasing -Werror=declaration-after-statement -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -arch i386 -arch x86_64 -g -Isrc -I/Library/Frameworks/Python.framework/Versions/3.4/include/python3.4m -c simple.cpp -o build/temp.macosx-10.6-intel-3.4/simple.o -Wno-unused-function -std=c++11
In file included from simple.cpp:289:
./simple.h:2:10: fatal error: 'array' file not found
#include <array>
^
1 error generated.
error: command '/usr/bin/clang' failed with exit status 1

$ /usr/bin/clang -fno-strict-aliasing -Werror=declaration-after-statement -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -arch i386 -arch x86_64 -g -Isrc -I/Library/Frameworks/Python.framework/Versions/3.4/include/python3.4m -c simple.cpp -o build/temp.macosx-10.6-intel-3.4/simple.o -Wno-unused-function -std=c++11

# no error!

$ /usr/bin/clang --version
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.4.0
Thread model: posix





setup.py:

#!/usr/bin/env python3

import platform, distutils.core, distutils.extension, Cython.Build

EXTENSION = distutils.extension.Extension(
name='simple',
sources=['simple.pyx'],
extra_compile_args=['-Wno-unused-function', '-std=c++11'],
language='c++',
)

EXT_MODULES=Cython.Build.cythonize(
[EXTENSION],
language='c++',
)

distutils.core.setup(
name='simple',
ext_modules=EXT_MODULES,
)





simple.pyx:

cdef extern from "simple.h" namespace "fcolor4":
struct Simple:
int x





simple.h:

int foo() {
auto x = 1; // works, so must be C++11
return x;
}

#include <string> // works, so must find some STL.
#include <array> // fails!

Answer

I believe this is the same problem I was having before here. In my case, the problem seemed to be that setuptools was using the same compiler flags as when the Python interpreter was compiled, and these included a flag that made the code compatible with a rather old version of Mac OS X (which was far before C++11).

Try adding the flag -mmacosx-version-min=10.9 (or later) to extra_compile_args in your setup.py.

Comments