Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!
like image 939
Tom Swirly Avatar asked Apr 30 '16 21:04

Tom Swirly


People also ask

Does Cython generate C code?

You need to add this path only if you use cimport numpy . With older Cython releases, setting this macro will fail the C compilation, because Cython generates code that uses this deprecated C-API.

How do I compile Cython files?

Run the cythonize command-line utility. This is a good approach for compiling a single Cython source file directly to an extension. A source file can be built “in place” (so that the extension module is created next to the source file, ready to be imported) with cythonize -i filename. pyx .

What compiler does Cython use?

Unlike most Python software, Cython requires a C compiler to be present on the system. The details of getting a C compiler varies according to the system used: Linux The GNU C Compiler (gcc) is usually present, or easily available through the package system.

How does Cython work?

Cython works by producing a standard Python module. However, the behavior differs from standard Python in that the module code, originally written in Python, is translated into C. While the resulting code is fast, it makes many calls into the CPython interpreter and CPython standard libraries to perform actual work.


1 Answers

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.

like image 192
jb326 Avatar answered Jan 02 '23 13:01

jb326