简体   繁体   English

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

[英]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脚本中的Extensioninclude_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包含所需的标题?

Update (11/22/2015) 更新(2015年11月22日)

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_argsextra_compile_args

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C / C ++与Python标准库的等价物 - C / C++ equivalents to the Python Standard Library 如何导入或安装预构建的python扩展模块(C ++)(即未通过setuptools编译的库)? - How to import or install pre-built python extension module (C++) (i.e. library not compiled via setuptools)? 使用外部库在C中创建Python类型:ctypes或setuptools? - Creating a Python type in C using an external library: ctypes or setuptools? 在 ipython 中用 Cython 包装 C++ 标准库 - Wrapping C++ Standard library with Cython in ipython setuptools:从 C++ 代码构建共享库,然后构建链接到共享库的 Cython 包装器 - setuptools: build shared library from C++ code, then build Cython wrapper linked to shared library 在Windows XP上从压缩标准库嵌入C ++中的Python 3.3 - Embedding Python 3.3 in C++ from zipped standard library on Windows XP 从C导入标准Python库 - Import standard Python library from C 使用setuptools时是否有任何理由列出标准库依赖项? - Is there any reason to list standard library dependencies when using setuptools? 使用Makefile在C ++中包含Python.h - Including Python.h in C++ with a Makefile python setuptools:ImportError:无法导入名称库 - python setuptools: ImportError: cannot import name Library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM