[Tutor] Compiling a program ::Embedding Python in C
Shadab Sayani
shadabsayani at yahoo.com
Tue Feb 13 07:01:11 CET 2007
Hi,
I have a C program mm.c calling python function as
follows::
#include "Python.h"
#include <stdio.h>
int main(int argc, char* argv[])
{
double answer = 0;
PyObject *modname, *mod, *mdict, *func,
*stringarg, *args, *rslt;
Py_Initialize();
modname = PyString_FromString("Test");
mod = PyImport_Import(modname);
if (mod)
{
mdict = PyModule_GetDict(mod);
func = PyDict_GetItemString(mdict,
"doit"); // borrowed reference
if (func)
{
if (PyCallable_Check(func))
{
stringarg =
PyString_FromString("5");//pay attention here
args = PyTuple_New(1);
PyTuple_SetItem(args,
0, stringarg);
rslt =
PyObject_CallObject(func, args);
if (rslt)
{
answer =
PyFloat_AsDouble(rslt);
Py_XDECREF(rslt);
}
Py_XDECREF(stringarg);
Py_XDECREF(args);
}
}
Py_XDECREF(mod);
}
Py_XDECREF(modname);
Py_Finalize();
printf("%g",answer);
return 0;
}
The python program -->(Test.py)
def doit(x1):
try:
x2 = eval(x1)
except:
print 'Error!'
return 0
else:
return x2
COMPILING-->
I used this make file to compile the above program
OPT=-I/usr/local/include/python2.4/ -DHAVE_CONFIG_H
-D_THREAD_SAFE -pthread -pipe
mm: clean mm.o
gcc $(OPT) -Wl.-E -o mm mm.o
/usr/local/lib/python2.4/config/libpython2.4.a -lm
mm.o: mm.c
gcc $(OPT) -c mm.c
clean:
rm -f *.o;rm -f *.so;rm -f *.core
But I got the following ERRORS-->
rm -f *.o;rm -f *.so;rm -f *.core
gcc -I/usr/local/include/python2.4/ -DHAVE_CONFIG_H
-D_THREAD_SAFE -pthread -pipe -c mm.c
gcc -I/usr/local/include/python2.4/ -DHAVE_CONFIG_H
-D_THREAD_SAFE -pthread -pipe -Wl.-E -o mm mm.o
/usr/local/lib/python2.4/config/libpython2.4.a -lm
/usr/local/lib/python2.4/config/libpython2.4.a(posixmodule.o)(.text+0x39a4):
In function `posix_tmpnam':
./Modules/posixmodule.c:6240: warning: the use of
`tmpnam_r' is dangerous, better use `mkstemp'
/usr/local/lib/python2.4/config/libpython2.4.a(posixmodule.o)(.text+0x3906):
In function `posix_tempnam':
./Modules/posixmodule.c:6195: warning: the use of
`tempnam' is dangerous, better use `mkstemp'
/usr/local/lib/python2.4/config/libpython2.4.a(dynload_shlib.o)(.text+0x15e):
In function `_PyImport_GetDynLoadFunc':
Python/dynload_shlib.c:130: undefined reference to
`dlopen'
/usr/local/lib/python2.4/config/libpython2.4.a(dynload_shlib.o)(.text+0x19f):Python/dynload_shlib.c:141:
undefined reference to `dlsym'
/usr/local/lib/python2.4/config/libpython2.4.a(dynload_shlib.o)(.text+0x1f6):Python/dynload_shlib.c:133:
undefined reference to `dlerror'
/usr/local/lib/python2.4/config/libpython2.4.a(posixmodule.o)(.text+0x1b49):
In function `posix_openpty':
: undefined reference to `openpty'
/usr/local/lib/python2.4/config/libpython2.4.a(posixmodule.o)(.text+0x1b93):
In function `posix_forkpty':
: undefined reference to `forkpty'
collect2: ld returned 1 exit status
make: *** [mm] Error 1
HOW TO FIX THESE and get a executable so that I can
directly run it?
Thanks and Regards,
Shadab.
Send instant messages to your online friends http://uk.messenger.yahoo.com
More information about the Tutor
mailing list