gcc -o pytest main -lpython2 -I usr include python2 include Python inc

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/* gcc -o pytest main.c -lpython2.5 -I/usr/include/python2.5/ */
#include <Python.h>
#include <stdio.h>
int
main (int argc, char **argv)
{
PyObject *module, *globals, *obj;
Py_Initialize();
module = PyImport_AddModule ("__main__");
globals = PyModule_GetDict (module);
obj = PyRun_String ("import math\na = math.sqrt(12)\n", Py_file_input, globals, globals);
if (obj == NULL)
{
PyErr_Print();
}
else
{
PyObject *result = PyDict_GetItemString (globals, "a");
float a;
PyArg_Parse (result, "f", &a);
printf ("variable `a': %.4f\n", a);
}
Py_DECREF (globals);
Py_Finalize();
return 0;
}