在Solaris中用Python调用外部C程序

2 投票
1 回答
633 浏览
提问于 2025-04-17 19:05

我正在尝试调用一个外部的C程序。这个代码在Linux和Windows上都能正常工作,但在Solaris上却不行。
有没有人能帮我看看?
最初的例子来自于 http://csl.name/C-functions-from-Python/
C代码(myModule.c)

#include <Python.h>

static PyObject* py_myFunction(PyObject* self, PyObject* args)
{
    char *s = "Hello from C!";
    return Py_BuildValue("s", s);
}

static PyObject* py_myOtherFunction(PyObject* self, PyObject* args)
{
    double x, y;
    PyArg_ParseTuple(args, "dd", &x, &y);
    return Py_BuildValue("d", x*y);
}

static PyMethodDef myModule_methods[] = {
    {"myFunction", py_myFunction, METH_VARARGS},
    {"myOtherFunction", py_myOtherFunction, METH_VARARGS},
    {NULL, NULL}
};

void initmyModule()
{
    (void) Py_InitModule("myModule", myModule_methods);
}

Python调用它

from myModule import *

print "Result from myFunction:", myFunction()
print "Result from myOtherFunction(4.0, 5.0):", myOtherFunction(4.0, 5.0)

在Linux上编译(在RHEL上测试过)

gcc -fPIC -shared -I/usr/include/python2.6 -lpython2.6 -o myModule.so myModule.c

在Windows XP下使用MinGW编译

gcc -Ic:/Python27/include -Lc:/Python27/libs myModule.c -lpython27 -shared -o myModule.pyd

但是我在Solaris上无法让它工作。我可以用以下命令编译它

gcc -fPIC -I/usr/include/python2.4 -L/usr/lib/python2.4 myModule.c -lpython2.4 -shared -o myModule.so

但它出现了一个错误

from myModule import *
ImportError: ld.so.1: python2.4: fatal: libgcc_s.so.1: open failed: No such file or directory

有没有人能帮我搞清楚这个问题?

gcc is 3.4.6
Python is 2.4.6
solaris 10 on x86 machine 

1 个回答

1

这应该能帮到你

 pfexec rm /usr/lib/libgcc_s.so.1
 pfexec ln -s /opt/ts/gcc/3.4/lib/libgcc_s.so.1 /usr/lib/libgcc_s.so.1

撰写回答