Question

I want make easy module with c-extension for python. I want just add two double values. But receive wrong result value. I can't find a mistake. my files:

add.c

#include "add.h"
double add(double a, double b){
    return a+b;
}

add.h

double add(double a, double b);

_add.c

#include <Python.h>
static char module_docstring[] =
    "This module provides an interface for calculating two integers using C.";
static char add_docstring[] =
    "Calculate the two int of some data given a model.";
static PyObject *add_add(PyObject *self, PyObject *args);

static PyMethodDef module_methods[] = {
    {"add", add_add, METH_VARARGS, add_docstring},
    {NULL, NULL, 0, NULL}
};

PyMODINIT_FUNC init_add(void)
{
    PyObject *m = Py_InitModule3("_add", module_methods, module_docstring);
    if (m == NULL)
        return;
}

static PyObject *add_add(PyObject *self, PyObject *args)
 {
     double a, b;
     /* Parse the input tuple */
     if (!PyArg_ParseTuple(args, "dd", &a, &b))
         return NULL;
     /* Call the external C function to compute the chi-squared. */
     double value = add(a, b);
     if (value < 0.0) {
         PyErr_SetString(PyExc_RuntimeError,
                     "Chi-squared returned an impossible value.");
         return NULL;
     }
     /* Build the output tuple */
     PyObject *res = Py_BuildValue("d", value);
     return res;
}

setup.py

from distutils.core import setup, Extension
setup(
    ext_modules=[Extension("_add", ["_add.c", "add.c"])]
)

i build module:

$ python setup.py build_ext --inplace

running build_ext building '_add' extension i686-linux-gnu-gcc
-pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c _add.c -o build/temp.linux-i686-2.7/_add.o
_add.c: In function ‘add_add’:
_add.c:30:6: warning: implicit declaration of function ‘add’ [-Wimplicit-function-declaration] i686-linux-gnu-gcc -pthread
-fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c add.c -o build/temp.linux-i686-2.7/add.o i686-linux-gnu-gcc -pthread -shared
-Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -D_FORTIFY_SOURCE=2 -g -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security build/temp.linux-i686-2.7/_add.o build/temp.linux-i686-2.7/add.o -o /home/nikolay/Documents/4COURSE-1/UNIX/lab3/cmodule/_add.so

and after i write in python:

 import _add
 print (_add.add(2.4, 3.5))

i receive: 1.0 Thanks for any help.

Was it helpful?

Solution

The warning about implicit implicit declaration of function ‘add’ is what's wrong: when compiling that source file, it thinks the function should be called in a different way than you've defined the function. If you include the add.h file in _add.c as well, it should work (does so on my machine).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top