Ubuntu - 链接 boost.python - 致命错误:找不到 pyconfig



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

你好世界 Python 扩展在 C++ 中使用 Boost?

我尝试在我的桌面上安装 boost,并且按照帖子在链接方面的建议完成。我有以下代码:

#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

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

解决方案是

apt-get install python-dev

在其他Linux风格中,你必须弄清楚如何安装python头。

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

find /usr/include -name pyconfig.h

[输出]:

/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 构建 boost 时也有类似的经历。我无法在我的系统上找到pyconfig.h,只有pyconfig-64.h。

搜索后,我发现您需要安装python-devel才能获得pyconfig.h

对于 CentOS,这样做:yum install python-devel .然后重试。

就我而言,我必须在我的目录/usr/include/中创建软链接

ln -s python3.5m python3.5

问题是我使用的是Python 3.5,但只有python3.5m目录存在,所以它找不到pyconfig.h文件。

如果您有多个 Python 安装,sysconfig 模块可以报告给定安装的 pyconfig.h 位置。

$ /path/to/python3 -c 'import sysconfig; print(sysconfig.get_config_h_filename())'
/path/to/pyconfig.h    

最新更新