Python setuptools不包括C ++标准库头

时间:2015-11-16 15:28:06

标签: python c++ macos setuptools

我正在尝试将Python包装器编译为我编写的小型C ++库。我编写了以下setup.py脚本来尝试使用setuptools来编译包装器:

from setuptools import setup, Extension
import numpy as np
import os

atmcmodule = Extension(
    'atmc',
    include_dirs=[np.get_include(), '/usr/local/include'],
    libraries=['mcopt', 'c++'],  # my C++ library is at ./build/libmcopt.a
    library_dirs=[os.path.abspath('./build')],
    sources=['atmcmodule.cpp'],
    language='c++',
    extra_compile_args=['-std=c++11', '-v'],
    )

setup(name='tracking',
      version='0.1',
      description='Particle tracking and MC optimizer module',
      ext_modules=[atmcmodule],
      )

但是,当我在OS X El Capitan上运行python setup.py build时,clang抱怨没有找到一些C ++标准库头:

In file included from atmcmodule.cpp:7:
In file included from ./mcopt.h:11:
In file included from ./arma_include.h:4:
/usr/local/include/armadillo:54:12: fatal error: 'initializer_list' file not found
  #include <initializer_list>
           ^
1 error generated.
error: command 'gcc' failed with exit status 1

-v标志传递给编译器会显示它正在搜索以下包含路径:

#include <...> search starts here:
 /Users/[username]/miniconda3/include
 /Users/[username]/miniconda3/lib/python3.4/site-packages/numpy/core/include
 /usr/local/include
 /Users/[username]/miniconda3/include/python3.4m
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/c++/4.2.1
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/c++/4.2.1/backward
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/7.0.0/include
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks (framework directory)
End of search list.

这显然不包括C ++标准库头的路径。如果我使用-v选项编译一个小的测试C ++源代码,我可以看到clang++通常也搜索路径/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1,如果我在include_dirs中包含此路径我的Extension脚本中的setup.py选项,然后扩展模块正确编译并运行。但是,将此路径硬编码到脚本中似乎不是一个好的解决方案,因为此模块还需要在Linux上运行。

所以,我的问题是如何正确地使setuptools包含所需的标题?

更新(2015年11月22日)

setuptools尝试编译扩展时,它会打印出它正在运行的第一个命令:

gcc -fno-strict-aliasing -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Users/[username]/miniconda3/include -arch x86_64 -I/Users/[username]/miniconda3/lib/python3.4/site-packages/numpy/core/include -I/Users/[username]/Documents/Code/ar40-aug15/monte_carlo/mcopt -I/usr/local/include -I/Users/[username]/miniconda3/include/python3.4m -c /Users/[username]/Documents/Code/ar40-aug15/monte_carlo/atmc/atmcmodule.cpp -o build/temp.macosx-10.5-x86_64-3.4/Users/[username]/Documents/Code/ar40-aug15/monte_carlo/atmc/atmcmodule.o -std=c++11 -fopenmp -v

如果我将此命令粘贴到终端并自行运行,则扩展程序会成功编译。所以我怀疑setuptools正在修改一些我不知道的环境变量,或者它对它实际运行的命令有点了解。

1 个答案:

答案 0 :(得分:3)

Setuptools尝试使用same flags used to compile the Python interpreter编译C / C ++扩展模块。在checking the flags用于编译我的Python安装(来自Anaconda)之后,我发现它正在编译10.5的最低Mac OS X版本。这似乎使它使用GCC libstdc ++而不是clang的libc ++(which supports C++11)。

可以通过将环境变量MACOSX_DEPLOYMENT_TARGET设置为10.9(或更高版本),或将'-mmacosx-version-min=10.9'添加到extra_compile_args来解决此问题。