Ubuntu – 链接boost.python – 致命错误:无法findpyconfig

有一些问题,现在我读了以下内容:

你好世界python扩展在c + +使用boost

我已经尝试安装提升到我的桌面上,并作为postbuild议的链接。 我有以下代码:

#include <boost/python.hpp> #include <Python.h> using namespace boost::python; 

现在我尝试了以下链接:

 g++ testing.cpp -I /usr/include/python2.7/pyconfig.h -L /usr/include/python2.7/Python.h -lpython2.7 

我也尝试了以下内容:

 g++ testing.cpp -I /home/username/python/include/ -L /usr/include/python2.7/Python.h -lpython2.7 

我不断收到以下错误:

 /usr/include/boost/python/detail/wrap_python.hpp:50:23: fatal error: pyconfig.h: No such file or directory # include <pyconfig.h> 

我不知道我哪里错了。 我有boost.python安装,只有一个问题链接?

我只是有同样的错误,问题是g ++无法找到pyconfig.h(令人震惊,我知道)。 对我来说,这个文件位于/usr/include/python2.7/pyconfig.h所以附加-I /usr/include/python2.7/应该修复它,或者你可以添加目录到你的路径:

 export CPLUS_INCLUDE_PATH="$CPLUS_INCLUDE_PATH:/usr/include/python2.7/" 

您也可以将其添加到您的.bashrc中,并且每当您启动您的shell时,它都会被添加(您将不得不重新打开终端以实现更改)。

你可以使用find /usr/include -name pyconfig.h找到你自己的python包含路径,在我的情况下会返回:

 /usr/include/python2.7/pyconfig.h /usr/include/i386-linux-gnu/python2.7/pyconfig.h 

如果您有一个.c文件( hello.c ),并且想要构建一个libhello.so库,请尝试:

 find /usr/include -name pyconfig.h 

[OUT]:

 /usr/include/python2.7/pyconfig.h /usr/include/x86_64-linux-gnu/python2.7/pyconfig.h 

然后使用输出并做:

 gcc -shared -o libhello.so -fPIC hello.c -I /usr/include/python2.7/ 

如果你从cython的.pyx转换到.so,试试这个python模块,它会自动生成.so文件给.pyx文件:

 def pythonizing_cython(pyxfile): import os # Creates ssetup_pyx.py file. setup_py = "\n".join(["from distutils.core import setup", "from Cython.Build import cythonize", "setup(ext_modules = cythonize('"+\ pyxfile+".pyx'))"]) with open('setup_pyx.py', 'w') as fout: fout.write(setup_py) # Compiles the .c file from .pyx file. os.system('python setup_pyx.py build_ext --inplace') # Finds the pyconfig.h file. pyconfig = os.popen('find /usr/include -name pyconfig.h'\ ).readline().rpartition('/')[0] # Builds the .so file. cmd = " ".join(["gcc -shared -o", pyxfile+".so", "-fPIC", pyxfile+".c", "-I", pyconfig]) os.system(cmd) # Removing temporary .c and setup_pyx.py files. os.remove('setup_pyx.py') os.remove(pyxfile+'.c') 

在为centos7建设时,我也有类似的经历。 我无法在我的系统上找到pyconfig.h,只有pyconfig-64.h。

搜索后,我发现你需要安装python-devel来获取pyconfig.h

这个症状有两个可能的原因:1.你没有安装python-dev。 2.你安装了python-dev,你的包含路径配置不正确,上面的贴子提供了一个解决方案。 在我的情况下,我正在安装boost,它正在寻找我的ubuntu中缺少的pyconfig.h头文件:

解决方案是

 apt-get install python-dev 

在其他的linux版本中,你必须弄清楚如何安装python头文件。