【问题标题】:Ubuntu - Linking boost.python - Fatal error: pyconfig cannot be foundUbuntu - 链接 boost.python - 致命错误:找不到 pyconfig
【发布时间】:2013-11-17 14:56:42
【问题描述】:

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

hello world python extension in c++ using 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,只是链接有问题?

【问题讨论】:

  • 首先,在我看到的所有示例中,在我的代码中,-I 和之后的路径之间没有空格(-L 也是如此)。第二,你确定你的路径是正确的吗?通常 python 包含文件在 /usr/include/python
  • 这些是编译器错误,而不是链接器错误。是否安装了 Python 开发者头文件?它们通常分布在开发者包中,例如python-dev

标签: c++ ubuntu gcc boost boost-python


【解决方案1】:

我刚刚遇到了同样的错误,问题是 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

【讨论】:

  • 另外,因为我不能直接评论你的问题(
  • @JaconHacker 您的解决方案对我有用。但是,我更喜欢使用-I /usr/include/python2.7。我熟悉make,但我应该在哪里将- I /usr/....... 添加到Jamfile
  • @Admia 对 jam 不太熟悉,但看起来它支持 cxxflags 参数,所以试试cxxflags=-I /usr/... 之类的东西
  • 只需安装 python-devel 即可解决此问题。
【解决方案2】:

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

解决办法是

apt-get install python-dev

在其他 linux 风格中,您必须弄清楚如何安装 python 头文件。

【讨论】:

  • apt-get install python3-dev,如果你有这种倾向`
  • python3-dev 没有解决我的问题。只有 python-dev 做到了。
【解决方案3】:

如果你有一个 .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 模块,它会根据 .pyx 文件自动构建 .so 文件:

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')

【讨论】:

  • 安装 python-devel 是正确的解决方案。
【解决方案4】:

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

找了一圈发现需要安装python-devel才能得到pyconfig.h

【讨论】:

  • 还需要fedora24:dnf install python-devel
【解决方案5】:

对于 CentOS,请执行以下操作:yum install python-devel。然后再试一次。

【讨论】:

    【解决方案6】:

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

    ln -s python3.5m python3.5
    

    问题是我使用的是 python 3.5,但只有 python3.5m 目录存在,因此无法找到 pyconfig.h 文件。

    【讨论】:

      【解决方案7】:

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

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

      【讨论】:

        猜你喜欢
        • 2017-01-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-26
        • 2019-10-09
        • 2013-01-21
        • 2014-10-03
        • 2020-02-17
        相关资源
        最近更新 更多