C++ Ubuntu - 链接 boost.python - 致命错误:找不到 pyconfig
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19810940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ubuntu - Linking boost.python - Fatal error: pyconfig cannot be found
提问by Phorce
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. I have the following code:
我已经尝试将 boost 安装到我的桌面上,并且按照链接方面的建议完成。我有以下代码:
#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,只是链接有问题?
回答by Jacob Hacker
I just had the same error, the problem is g++ can't find pyconfig.h(shocking, I know). 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:
我刚刚遇到了同样的错误,问题是 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/"
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:
您可以使用 找到自己的 python 包含路径find /usr/include -name pyconfig.h
,在我的情况下返回:
/usr/include/python2.7/pyconfig.h
/usr/include/i386-linux-gnu/python2.7/pyconfig.h
回答by Kemin Zhou
There two possible causes for this symptom: 1. you don't have python-dev installed. 2. you have python-dev installed and your include path is incorrectly configured, which the above posting provide a solution. In my case, I was installing boost, and it is looking for the pyconfig.h header file that is missing in my ubuntu:
此症状有两个可能的原因: 1. 您没有安装 python-dev。2.你安装了python-dev并且你的包含路径配置不正确,上面的帖子提供了一个解决方案。就我而言,我正在安装 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 头文件。
回答by alvas
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')
回答by Ben
I had a similar experience when building boost for centos7. I was not able to find pyconfig.h on my system only pyconfig-64.h.
我在为 centos7 构建 boost 时也有类似的经历。我无法在我的系统上找到 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
回答by Gamma.X
For CentOS do this: yum install python-devel
. Then try again.
对于 CentOS,请执行以下操作:yum install python-devel
. 然后再试一次。
回答by Mitch Chapman
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
回答by Alex
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.hfile.
问题是我使用的是 python 3.5,但只有 python3.5m 目录存在,所以它无法找到pyconfig.h文件。