2015-11-16 61 views
1

我嘗試編譯Python包裝,以++庫,我寫了一個小C。我已經寫了下面的setup.py腳本來嘗試使用setuptools編譯包裝:Python的setuptools的不包括C++標準庫頭

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埃爾卡皮坦運行python setup.py buildclang抱怨沒有找到一些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選項中,那麼擴展模塊將正確編譯並工作。但是,將此路徑硬編碼到腳本似乎不是一個好的解決方案,因爲此模塊也需要在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

setuptools的試圖編譯與所述same flags used to compile the Python interpreter C/C++擴展模塊。用於編譯我的Python(從Anaconda)的安裝後checking the flags,我發現這是一種編譯10.5最小的Mac OS X版本。這似乎使它使用GCC libstdC++代替了clang的libC++(which supports C++11)。

這可以通過設置環境變量MACOSX_DEPLOYMENT_TARGET10.9(或更高版本),或添加到'-mmacosx-version-min=10.9'extra_compile_args被固定。