我想在Mac中编译一个简单的C扩展以便与Python一起使用,并且在命令行中一切正常。下面介绍可用的代码和gcc命令。 现在我试图在Xcode 4.5(Mac OS10.8)中构建相同的扩展,并且我尝试了dylib或静态库的几个目标设置,但是我总是得到一个无法用Python加载的文件,显示错误:在Xcode for Mac中为Python编译和链接C扩展
./myModule.so: unknown file type, first eight bytes: 0x21 0x3C 0x61 0x72 0x63 0x68 0x3E 0x0A
我最终的目标是在XCode中用C/C++扩展的源代码创建一个工作空间,并使用python脚本在Xcode中调用它。所以,如果我需要调试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);
}
前8个字节解码为“! \ n”。这对你来说意味着什么? –