[英]Python setuptools not including C++ standard library headers
I'm trying to compile a Python wrapper to a small C++ library I've written. 我正在尝试将Python包装器编译为我编写的小型C ++库。 I've written the following setup.py
script to try to use setuptools
to compile the wrapper: 我编写了以下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],
)
However, when I run python setup.py build
on OS X El Capitan, clang
complains about not finding some C++ standard library headers: 但是,当我在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
Passing the -v
flag to the compiler shows that it is searching the following include paths: 将-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.
This apparently doesn't include the path to the C++ standard library headers. 这显然不包括C ++标准库头的路径。 If I compile a small test C++ source with the -v
option, I can see that clang++
normally also searches the path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1
, and if I include this path in the include_dirs
option for Extension
in my setup.py
script, then the extension module compiles correctly and works. 如果我使用-v
选项编译一个小的测试C ++源代码,我可以看到clang++
通常也会搜索路径/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1
,如果我在setup.py
脚本中的Extension
的include_dirs
选项中包含此路径,则扩展模块可以正确编译并运行。 However, hard-coding this path into the script doesn't seem like a good solution since this module also needs to work on Linux. 但是,将此路径硬编码到脚本中似乎不是一个好的解决方案,因为此模块还需要在Linux上运行。
So, my question is how do I properly make setuptools
include the required headers? 所以,我的问题是如何正确地使setuptools
包含所需的标题?
As setuptools
tries to compile the extension, it prints the first command it's running: 当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
If I paste this command into a terminal and run it myself, the extension compiles successfully. 如果我将此命令粘贴到终端并自行运行,则扩展程序将成功编译。 So I suspect either setuptools
is modifying some environment variables I'm not aware of, or it's lying a little about the commands it's actually running. 所以我怀疑setuptools
是修改了一些我不知道的环境变量,或者它对它实际运行的命令有点了解。
Setuptools tries to compile C/C++ extension modules with the same flags used to compile the Python interpreter . Setuptools尝试使用与编译Python解释器相同的标志来编译C / C ++扩展模块。 After checking the flags used to compile my Python install (from Anaconda), I found it was compiling for a minimum Mac OS X version of 10.5. 在检查用于编译我的Python安装的标志 (来自Anaconda)后,我发现它正在编译10.5的最低Mac OS X版本。 This seems to make it use the GCC libstdc++ instead of clang's libc++ ( which supports C++11 ). 这似乎使它使用GCC libstdc ++而不是clang的libc ++( 它支持C ++ 11 )。
This can be fixed by either setting the environment variable MACOSX_DEPLOYMENT_TARGET
to 10.9
(or later), or adding '-mmacosx-version-min=10.9'
to extra_compile_args
. 这可以通过将环境变量MACOSX_DEPLOYMENT_TARGET
设置为10.9
(或更高版本),或将'-mmacosx-version-min=10.9'
到extra_compile_args
来extra_compile_args
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.