Embedding Python

Programs can use libpython to run the Python interpreter

Trivial example (embed0.c):

#include <Python.h>

main(int argc,char **argv)
    {
    Py_Initialize();
    PyRun_SimpleString("import time\n"
                       "print 'time =', time.time()\n");
    Py_Finalize();
    }

Compile with the command:

cc -Xlinker -export-dynamic -I/usr/include/python embed0.c -o embed0 -L/usr/lib/python/config/ -lpython2.2 -ldl -lpthread -lutil -lm

The flags and libraries that a program needs to be linked with can be found using the script Linking.py


next