I am trying to compile a simple C extension on Mac for use with Python, and everything works fine on the command line. Below are the Code and gcc commands that work. Now I'm trying to create the same extension in Xcode 4.5 (Mac OS10.8), and I tried several target settings for dylib or static library, but I always get a file that could not be loaded in Python, showing an error:
./myModule.so: unknown file type, first eight bytes: 0x21 0x3C 0x61 0x72 0x63 0x68 0x3E 0x0A
My ultimate goal is to create a workspace in Xcode with the source code for the C / C ++ extension and have a python script that calls it in Xcode. So, if I need to debug the C / C ++ extension, I have the ability to debug Xcode. I know that Xcode is not debugging in a Python script, but it can run it, right?
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); }
source share