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

Pho*_*rce 38 c++ ubuntu gcc boost boost-python

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

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

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

#include <boost/python.hpp>
#include <Python.h>
using namespace boost::python;
Run Code Online (Sandbox Code Playgroud)

现在我尝试连接以下内容:

g++ testing.cpp -I /usr/include/python2.7/pyconfig.h -L /usr/include/python2.7/Python.h
-lpython2.7
Run Code Online (Sandbox Code Playgroud)

我也尝试了以下内容:

g++ testing.cpp -I /home/username/python/include/ -L /usr/include/python2.7/Python.h -lpython2.7
Run Code Online (Sandbox Code Playgroud)

我一直收到以下错误:

/usr/include/boost/python/detail/wrap_python.hpp:50:23: fatal error: pyconfig.h: No such   
file or directory
# include <pyconfig.h>
Run Code Online (Sandbox Code Playgroud)

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

Jac*_*ker 77

我只是有同样的错误,问题是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/"
Run Code Online (Sandbox Code Playgroud)

您也可以将它添加到.bashrc中,并且每当您下次启动shell时都会添加它(您必须重新打开终端才能实现更改).

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

/usr/include/python2.7/pyconfig.h
/usr/include/i386-linux-gnu/python2.7/pyconfig.h
Run Code Online (Sandbox Code Playgroud)

  • 只需安装python-devel就可以解决这个问题. (3认同)

Kem*_*hou 12

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

解决方案是

apt-get install python-dev
Run Code Online (Sandbox Code Playgroud)

在其他Linux版本中,你必须弄清楚如何安装python头.

  • 或者`apt-get install python3-dev`如果你那样倾向于` (4认同)

alv*_*vas 7

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

find /usr/include -name pyconfig.h
Run Code Online (Sandbox Code Playgroud)

[OUT]:

/usr/include/python2.7/pyconfig.h
/usr/include/x86_64-linux-gnu/python2.7/pyconfig.h
Run Code Online (Sandbox Code Playgroud)

然后使用输出并执行:

gcc -shared -o libhello.so -fPIC hello.c -I /usr/include/python2.7/
Run Code Online (Sandbox Code Playgroud)

如果你要从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')
Run Code Online (Sandbox Code Playgroud)


Ben*_*Ben 7

我在为centos7构建提升时有过类似的经历.我无法在我的系统上找到pyconfig.h只有pyconfig-64.h.

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


Gam*_*a.X 5

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