简体   繁体   English

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

[英]Ubuntu - Linking boost.python - Fatal error: pyconfig cannot be found

Having some issues, now I have read the following:有一些问题,现在我已阅读以下内容:

hello world python extension in c++ using boost? hello world python 扩展在 C++ 中使用 boost?

I have tried installing boost onto my desktop, and, done as the posts suggested in terms of linking.我已经尝试将 boost 安装到我的桌面上,并且按照链接方面的建议完成。 I have the following code:我有以下代码:

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

Now I have tried linking with the following:现在我尝试与以下链接:

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

And I have tried the following as well:我也尝试了以下方法:

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

I keep getting the following error:我不断收到以下错误:

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

I don't know where I am going wrong.我不知道我哪里出错了。 I do have boost.python installed, there's just a problem linking?我确实安装了 boost.python,只是链接有问题?

I just had the same error, the problem is g++ can't find pyconfig.h(shocking, I know).我刚刚遇到了同样的错误,问题是 g++ 找不到 pyconfig.h(令人震惊,我知道)。 For me this file is located in /usr/include/python2.7/pyconfig.h so appending -I /usr/include/python2.7/ should fix it, alternatively you can add the directory to your path with:对我来说,这个文件位于/usr/include/python2.7/pyconfig.h所以附加-I /usr/include/python2.7/应该修复它,或者你可以将目录添加到你的路径中:

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

You can also add this to your .bashrc and it will be added whenever you start your shell next(you will have to reopen your terminal to realize the changes).您也可以将它添加到您的 .bashrc 中,它会在您下次启动 shell 时添加(您必须重新打开终端才能实现更改)。

You can find your own python include path by using find /usr/include -name pyconfig.h , in my case this returns:您可以使用find /usr/include -name pyconfig.h找到自己的 python 包含路径,在我的情况下返回:

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

There two possible causes for this symptom: 1. you don't have python-dev installed.此症状有两个可能的原因: 1. 您没有安装 python-dev。 2. you have python-dev installed and your include path is incorrectly configured, which the above posting provide a solution. 2.你安装了python-dev并且你的包含路径配置不正确,上面的帖子提供了一个解决方案。 In my case, I was installing boost, and it is looking for the pyconfig.h header file that is missing in my ubuntu:就我而言,我正在安装 boost,它正在寻找我的 ubuntu 中缺少的 pyconfig.h 头文件:

The solution is解决办法是

apt-get install python-dev

In other linux flavors, you have to figure out how to install python header.在其他 linux 版本中,您必须弄清楚如何安装 python 头文件。

If you have a .c file ( hello.c ) and you want to build an libhello.so library, try:如果您有一个.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

then use the output and do:然后使用输出并执行:

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

If you're converting from cython's .pyx to .so, try this python module, it will automatically build the .so file given the .pyx file:如果您要从 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')

I had a similar experience when building boost for centos7.我在为 centos7 构建 boost 时也有类似的经历。 I was not able to find pyconfig.h on my system only pyconfig-64.h.我无法在我的系统上找到 pyconfig.h,只有 pyconfig-64.h。

After searching around I found that you need to install python-devel to get pyconfig.h找了一圈后发现需要安装python-devel才能得到pyconfig.h

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

In my case, I had to create a soft link in my dir /usr/include/就我而言,我必须在我的目录/usr/include/ 中创建一个软链接

ln -s python3.5m python3.5

the probleme was that i was using python 3.5 but only the python3.5m directory was existing so it wasn't able to find the pyconfig.h file.问题是我使用的是 python 3.5,但只有 python3.5m 目录存在,所以它无法找到pyconfig.h文件。

In case you have multiple Python installations, the sysconfig module can report the location of pyconfig.h for a given installation.如果您有多个 Python 安装,sysconfig 模块可以报告给定安装的 pyconfig.h 的位置。

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM