简体   繁体   English

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

[英]Python.h header file missing on Mac OS X 10.6

I'm trying to access a shared C library in Python with ctypes on Mac OS X 10.6.8 with Python 2.7.4. 我正在尝试使用Python 2.7.4在Mac OS X 10.6.8上使用ctypes访问Python中的共享C库。 To do this, I need to #include <Python.h> in my C code. 为此,我需要在C代码中#include <Python.h> If I try to compile a C script that only has that one include statement in it, call it "sample.c", I get: 如果我尝试编译仅包含一个include语句的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 is a framework on Mac OS X so you need to, Python是Mac OS X上的框架,因此您需要

#include <Python/Python.h>

You also need to call gcc with the -framework argument to actually do anything inside C, 您还需要使用-framework参数调用gcc以在C中实际执行任何操作,

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

I'm not sure about 10.6.8, but Python.h should 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. 如果您安装了官方的python.org二进制文件。 Try adding 尝试添加

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

to your gcc command and see if that works. 到您的gcc命令,看看是否可行。

In case you have installed Python using Brew, it may be worthwhile to check the location of where your headers are. 如果您使用Brew安装了Python,则可能值得检查标头的位置。 Try I/usr/local/Cellar/python/... 试试I/usr/local/Cellar/python/...

Another way is to add `python-config --include` to the gcc call. 另一种方法是在gcc调用中添加`python-config --include` It will expand to -I/usr/... , so 它将扩展为-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`

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

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