我正在尝试在 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
我的最终目标是使用 C/C++ 扩展的源代码在 XCode 中创建一个工作区,并让 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);
}
最佳答案
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. Starting a new BSD dynamic library project solved the issues I was seeing. Thanks for the help!
关于python - 在 Xcode for Mac 中编译和链接 Python 的 C 扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13324336/