LINUX.ORG.RU

python, c api, линковка исходников

 ,


0

1

не получается собрать

some-dir/cext/add.c

#include "arth.h"

static PyObject* add(PyObject* self, PyObject* args) {
    int a, b;

    if (!PyArg_ParseTuple(args, "ii", &a, &b))
	return NULL;
    
    return Py_BuildValue("(i)", a+b);
}
some-dir/cext/mult.c
#include "arth.h"

static PyObject* add(PyObject* self, PyObject* args) {
    int a, b;

    if (!PyArg_ParseTuple(args, "ii", &a, &b))
	return NULL;
    
    return Py_BuildValue("(i)", a*b);
}
some-dir/cext/arth.h
#include <Python.h>

static PyObject* add(PyObject* self, PyObject* args);
static PyObject* mult(PyObject* self, PyObject* args);
some-dir/cext/arth.c
#include "arth.h"

static PyMethodDef ArthMethods[] = {
    {"add", add, METH_VARARGS, "a + b"},
    {"mult", mult, METH_VARARGS, "a * b"},
    {NULL, NULL, 0, NULL}
};

static struct PyModuleDef arth_module = {
    PyModuleDef_HEAD_INIT,
    "arth",   
    NULL, 
    -1,       
    ArthMethods
};
 
PyMODINIT_FUNC PyInit_arth(void)
{
    return PyModule_Create(&arth_module);
}
some-dir/setup.py
from distutils.core import setup, Extension

module1 = Extension('acc',
                    sources=['cext/add.c',
                             'cext/mult.c',
                             'cext/arth.c'])

setup(ext_modules = [module1])

компиляция

python3 setup.py build_ext --inplace
running build_ext
building 'acc' extension
gcc -pthread -Wno-unused-result -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/usr/local/include/python3.3m -c cext/add.c -o build/temp.linux-i686-3.3/cext/add.o
cext/add.c:3:18: warning: ‘add’ defined but not used [-Wunused-function]
cext/arth.h:4:18: warning: ‘mult’ declared ‘static’ but never defined [-Wunused-function]
gcc -pthread -Wno-unused-result -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/usr/local/include/python3.3m -c cext/mult.c -o build/temp.linux-i686-3.3/cext/mult.o
cext/mult.c:3:18: warning: ‘add’ defined but not used [-Wunused-function]
cext/arth.h:4:18: warning: ‘mult’ declared ‘static’ but never defined [-Wunused-function]
gcc -pthread -Wno-unused-result -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/usr/local/include/python3.3m -c cext/arth.c -o build/temp.linux-i686-3.3/cext/arth.o
cext/arth.h:3:18: warning: ‘add’ used but never defined [enabled by default]
cext/arth.h:4:18: warning: ‘mult’ used but never defined [enabled by default]
gcc -pthread -shared build/temp.linux-i686-3.3/cext/add.o build/temp.linux-i686-3.3/cext/mult.o build/temp.linux-i686-3.3/cext/arth.o -o /home/username/some-dir/acc.cpython-33m.so

Compilation finished at Sat Nov 16 19:04:00
запуск some-dir/test.py
import acc
print(acc.add(1,2))
----> 1 import acc
      2 print(acc.add(1,2))
      3 

ImportError: ./acc.cpython-33m.so: undefined symbol: add

что я делаю не так?

Ответ на: комментарий от qweqwe

это ошибка линкера фактически - почитайте что делает static

AIv ★★★★★ ()
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.