我试图编译在Mac的一个简单的C扩展与Python的使用,以及所有在命令行中运行良好。 代码和gcc命令的作品介绍如下。 现在我想建立在Xcode 4.5(Mac的OS10.8)相同的分机,我尝试了两种dylib或静态库中的多个目标的设置,但我总是无法在Python加载显示错误文件:
./myModule.so: unknown file type, first eight bytes: 0x21 0x3C 0x61 0x72 0x63 0x68 0x3E 0x0A
我的最终目标是要在Xcode中创建一个工作区与C / C ++扩展的源代码,并有一个调用它在Xcode python脚本。 所以,如果我需要调试C / C ++的扩展我的XCode调试功能。 我知道的XCode没有调试到Python脚本,但它可以运行它,对吗?
gcc -shared -arch i386 -arch x86_64 -L/usr/lib/python2.7 -framework python -I/usr/include/python2.7 -o myModule.so myModule.c -v
#include <Python.h>
/*
* Function to be called from Python
*/
static PyObject* py_myFunction(PyObject* self, PyObject* args)
{
char *s = "Hello from C!";
return Py_BuildValue("s", s);
}
/*
* Another function to be called from Python
*/
static PyObject* py_myOtherFunction(PyObject* self, PyObject* args)
{
double x, y;
PyArg_ParseTuple(args, "dd", &x, &y);
return Py_BuildValue("d", x*y);
}
/*
* Bind Python function names to our C functions
*/
static PyMethodDef myModule_methods[] = {
{"myFunction", py_myFunction, METH_VARARGS},
{"myOtherFunction", py_myOtherFunction, METH_VARARGS},
{NULL, NULL}
};
/*
* Python calls this to let us initialize our module
*/
void initmyModule()
{
(void) Py_InitModule("myModule", myModule_methods);
}