[Q] How Can I call python function from C source?

Kim Chul Min cmkim at shinbiro.com
Fri Jun 28 02:11:16 EDT 2002


Thank you for the help, Dave.

BTW, I make test1.c like what you said and when I compile it I got some
compile errors. Plz help more.

**************************************************************
$ gcc -o test1 test1.c
Undefined                       first referenced
 symbol                             in file
PyCallable_Check                    /var/tmp/ccob3Tkv.o
PyLong_AsLong                       /var/tmp/ccob3Tkv.o
PyObject_HasAttrString              /var/tmp/ccob3Tkv.o
PyObject_GetAttrString              /var/tmp/ccob3Tkv.o
Py_Exit                             /var/tmp/ccob3Tkv.o
PyImport_ImportModule               /var/tmp/ccob3Tkv.o
PyObject_CallFunction               /var/tmp/ccob3Tkv.o
Py_Initialize                       /var/tmp/ccob3Tkv.o
ld: fatal: Symbol referencing errors. No output written to main
collect2: ld returned 1 exit status
**************************************************************

Anything I mistake?
Wait for Any Idea. Plz help me.

Kim Chul Min <cmkim at shinbiro.com>

"Dave Kuhlman" <dkuhlman at rexx.com> wrote in message
news:affguf$e3cn5$1 at ID-139865.news.dfncis.de...
> Kim Chul Min wrote:
>
> > Hello there,
> >
> > I want to call python function from c source.
> >
> > for example,
> >
> > in file "main.c"
> > --------------------------------------------------------------
> > int main(void)
> > {
> >
> >     /** want to call python function add(a,b) that return a+b **/
> >
> > }
> > --------------------------------------------------------------
> >
> > in the file "add.py"
> > --------------------------------------------------------------
> > def add(a,b):
> >     return(a+b)
> > --------------------------------------------------------------
> >
> > Can I call any function that written by python from C source ?
> >
> > If possible, can anyone show me simple example like above??
> >
> > Plz,,, Help me....Thankx in advance...
> >
> > Ciao,
> > Farangan Xavio
> > cmkim at shinbiro.com
>
> Here is a simple example.
>
> You will find the following especially helpful:
>
>     http://www.python.org/doc/current/api/importing.html
>     http://www.python.org/doc/current/api/object.html
>
> /* =========================================== */
>
> #include "Python.h"
>
> int main(int argc, char ** argv)
> {
>     PyObject * module;
>     PyObject * function;
>     PyObject * result;
>     long cresult;
>     int arg1;
>     int arg2;
>
>     if (argc != 3)
>     {
>         printf("Usage: test1 <arg1> <arg2>\n");
>         exit(-1);
>     } /* if */
>
>     arg1 = atoi(argv[1]);
>     arg2 = atoi(argv[2]);
>
>     Py_Initialize();
>     module = PyImport_ImportModule("add");
>     if (PyObject_HasAttrString(module, "add"))
>     {
>         function = PyObject_GetAttrString(module, "add");
>         if (PyCallable_Check(function))
>         {
>             result = PyObject_CallFunction(function, "ii", arg1, arg2);
>             cresult = PyLong_AsLong(result);
>             printf("arg1: %d  arg2: %d  result: %d\n", arg1, arg2,
cresult);
>         }
>         else
>         {
>             printf("Error -- attribute is not a function/method\n");
>         } /* if */
>     }
>     else
>     {
>         printf("Error -- Module does not contain \"add\" function.\n");
>     } /* if */
>     Py_Exit(0);
> } /* main */
>
> /* =========================================== */
>
>
>
> --
> Dave Kuhlman
> dkuhlman at rexx.com
> http://www.rexx.com/~dkuhlman
>





More information about the Python-list mailing list