Mac OS X 10.6 上缺少 Python.h 头文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16454129/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:40:17  来源:igfitidea点击:

Python.h header file missing on Mac OS X 10.6

pythonxcodemacososx-snow-leopardheader-files

提问by Brett Morris

I'm trying to access a shared C library in Python with ctypeson Mac OS X 10.6.8 with Python 2.7.4. To do this, I need to #include <Python.h>in my C code. If I try to compile a C script that only has that one include statement in it, call it "sample.c", I get:

我正在尝试ctypes使用 Python 2.7.4 在 Mac OS X 10.6.8 上访问 Python 中的共享 C 库。为此,我需要#include <Python.h>在我的 C 代码中。如果我尝试编译一个只有一个包含语句的 C 脚本,将其命名为“sample.c”,我得到:

$ gcc -shared -o sample.so sample.c
sample.c:1:20: error: Python.h: No such file or directory

Since I'm running Mac 10.6, I have Xcode 3.2.6, the latest version available on this iteration of OS X without paying to upgrade to 10.7 and getting Xcode 4. Is there a way to get the Python header file without upgrading my OS?

因为我运行的是 Mac 10.6,所以我有 Xcode 3.2.6,这是 OS X 的最新版本,无需支付升级到 10.7 和获得 Xcode 4 的费用。有没有办法在不升级我的操作系统的情况下获取 Python 头文件?

采纳答案by Jared

Python is a framework on Mac OS X so you need to,

Python 是 Mac OS X 上的一个框架,所以你需要,

#include <Python/Python.h>

You also need to call gccwith the -frameworkargument to actually do anything inside C,

您还需要gcc使用-framework参数调用以在 C 中实际执行任何操作,

gcc -shared -o sample.so sample.c -framework Python

回答by MattDMo

I'm not sure about 10.6.8, but Python.hshould be in

我不确定 10.6.8,但Python.h应该在

/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7

if you installed the official python.org binary. Try adding

如果您安装了官方的 python.org 二进制文件。尝试添加

-I/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7

to your gcccommand and see if that works.

到你的gcc命令,看看是否有效。

回答by suryashekhar

In case you have installed Python using Brew, it may be worthwhile to check the location of where your headers are. Try I/usr/local/Cellar/python/...

如果您已经使用 Brew 安装了 Python,那么检查头文件所在的位置可能是值得的。尝试I/usr/local/Cellar/python/...

回答by ttq

Another way is to add `python-config --include`to the gcccall. It will expand to -I/usr/..., so

另一种方法是添加`python-config --include`gcc呼叫中。它将扩展到-I/usr/...,所以

gcc -shared -o sample.so sample.c `python-config --include`

Also other options can be retrieved, such as `python-config --cflags --ldflags`.

还可以检索其他选项,例如`python-config --cflags --ldflags`.