Compiling and linking the C extension for Python in Xcode for Mac

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); } 
+3
source share
2 answers

This guy seems to have the same problem .

I understood the problem. Although I changed the parameter in xcode to indicate the output type is “dynamic library” or “bundle,” xcode ignored the parameter. Starting a new BSD dynamic library project solved the problems I saw. Thanks for the help!

+3
source

I have had successful debugging of C extensions tested by modules in Xcode 4.6 using setuptools, virtualenv, unittest and GDB as a debugger.

I use virtualenvwrapper to create virtualenv for the project, and then set ~ / .virtualenvs / module_name / bin / python as the debug executable.

The only argument to go to the python virtualenv interpreter in the Run configuration is the path to your test.py.

Then I install GDB, not None, since the debugger starts it automatically.

The final step is to pass "setup.py install" as arguments to your build tool (~ / .virtualenvs / module_name / bin / python) in the "Set Up an External Tool" panel of the test target. Virtualenv provides a fairly easy way to install a common object for extending C into the test path of the python script interpreter library without actually installing it in the global site packages for your host.

With this setting, I can call the extension code from a python script (the final goal) and still debug the C code using the Xcode GUI debugging support.

If I have not described this, please let me know and I will tell you an example project.

0
source

Source: https://habr.com/ru/post/1203779/


All Articles