简体   繁体   English

在 Xcode for Mac 中为 Python 编译和链接 C 扩展

[英]Compiling and linking C extension for Python in Xcode for Mac

I am trying to compile a simple C extension in Mac to use with Python, and all works well in the command line.我正在尝试在 Mac 中编译一个简单的 C 扩展以与 Python 一起使用,并且在命令行中一切正常。 Code and gcc command that works are presented below.有效的代码和 gcc 命令如下所示。 Now I am trying to build the same extension in Xcode 4.5 (Mac OS10.8), and I tried several target settings for either dylib or static library, but I always get a file that cannot be loaded in Python showing the error:现在我试图在 Xcode 4.5 (Mac OS10.8) 中构建相同的扩展,我尝试了 dylib 或静态库的几个目标设置,但我总是得到一个无法在 Python 中加载的文件,显示错误:

./myModule.so: unknown file type, first eight bytes: 0x21 0x3C 0x61 0x72 0x63 0x68 0x3E 0x0A

My ultimate target is to create a workspace in XCode with the source code of a C/C++ extension and have python script that calls it in Xcode.我的最终目标是使用 C/C++ 扩展的源代码在 XCode 中创建一个工作区,并拥有在 Xcode 中调用它的 python 脚本。 So, if I need to debug the C/C++ extension I have XCode debugging capabilities.所以,如果我需要调试 C/C++ 扩展,我有 XCode 调试功能。 I am aware that XCode do not debug into Python script, but it can run it, correct ?我知道 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);
}

This guy seems to be having the same problem . 这家伙似乎也有同样的问题

I've figured out the problem.我已经想通了问题所在。 Even though I changed the setting in xcode to specify output type "dynamic library" or "bundle", xcode was ignoring the setting.即使我更改了 xcode 中的设置以指定输出类型“动态库”或“捆绑”,xcode 也忽略了该设置。 Starting a new BSD dynamic library project solved the issues I was seeing.启动一个新的 BSD 动态库项目解决了我所看到的问题。 Thanks for the help!谢谢您的帮助!

I've had success debugging unit-tested C extensions in XCode 4.6 using setuptools, virtualenv, unittest and GDB as the debugger.我已经成功地使用 setuptools、virtualenv、unittest 和 GDB 作为调试器在 XCode 4.6 中调试了经过单元测试的 C 扩展。

I use virtualenvwrapper to create a virtualenv for the project and then set ~/.virtualenvs/module_name/bin/python as the executable to debug.我使用 virtualenvwrapper 为项目创建一个 virtualenv,然后将 ~/.virtualenvs/module_name/bin/python 设置为要调试的可执行文件。

The single argument to pass to the virtualenv python interpreter in the Run configuration is the path to your test.py.在 Run 配置中传递给 virtualenv python 解释器的单个参数是 test.py 的路径。

I then set GDB rather than None as the debugger launching it automatically.然后我将 GDB 而不是 None 设置为自动启动它的调试器。

The last step is to pass "setup.py install" as the arguments to your build tool (~/.virtualenvs/module_name/bin/python) on your test target's External Build Tool Configuration pane.最后一步是将“setup.py install”作为参数传递给测试目标的外部构建工具配置窗格上的构建工具 (~/.virtualenvs/module_name/bin/python)。 The virtualenv provides a fairly simple way for you to install the shared object for your C extension into the test script python interpreter's library path without actually installing it into the global site-packages for your host. virtualenv 为您提供了一种相当简单的方法,可以将 C 扩展的共享对象安装到测试脚本 python 解释器的库路径中,而无需将其实际安装到主机的全局站点包中。

With this setup I can call the extension code from a python script (the ultimate aim) and still debug the C code using XCode's GUI debug support.通过此设置,我可以从 python 脚本(最终目标)调用扩展代码,并且仍然使用 XCode 的 GUI 调试支持来调试 C 代码。

If I haven't described this clearly please let me know and I'll share an example project.如果我没有清楚地描述这一点,请告诉我,我将分享一个示例项目。

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

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