Cython构建可以'找不到C++11 STL文件,但仅当从setup.py调用时



当我从setup.py编译代码时,它找不到C++11包含文件<array>-但C++11编译器功能确实有效。

当我把setup.py生成的同一个命令行粘贴到我的shell中时,它都编译得很好(!)

演示这种行为的代码可以在这里看到,也可以粘贴在下面。


终端会话:

$ 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!

我相信这和我之前遇到的问题是一样的。在我的案例中,问题似乎是setuptools使用了与编译Python解释器时相同的编译器标志,其中包括一个标志,该标志使代码与相当旧的Mac OS X版本(远早于C++11)兼容。

尝试将标志-mmacosx-version-min=10.9(或更高版本)添加到setup.py中的extra_compile_args

最新更新