System Approach
We could call Python from C program by using the system function, which basically means, we do need have an already written python program, or you would need to make a C program write a python program to disk. Here is a quick example:
Expand|Select|Wrap|Line Numbers
- #include <stdio.h>
- int main(){
- char *prg = (char*)"print \"hello from python\"\n";
- FILE *f= (FILE*)fopen("test2.py","w");
- fprintf(f, prg);
- fclose(f);
- system("python test2.py");
- }
gcc test1.c
Then you would run it like this:
./a.out
The bad thing about this approach is; what if we were to do anything in python, and we wanted Python to output something, which we then could process in C? We could make our Python program write to file, then we could have the C program read the outputted file from the Python program, but then you have to make some modifications to your python program and you would have a lot of files with result floating around on your harddrive (like there isn't enough there already!)
Process Approach
Let's make a toy program, we want to add two number in Python, which are given to us from a C program. So the C program would supply us with the two numbers, the two numbers would then be sent to a Python program and the result would be written from the C program through the printf command.
Expand|Select|Wrap|Line Numbers
- #include <stdio.h>
- #include <stdlib.h>
- int main(){
- int number1 = 10;
- int number2 = 32;
- int number3 = -1;
- /* the python program as a C string */
- char *prg = (char*)"import sys\nprint int(sys.argv[1])+int(sys.argv[2])\n";
- /* make a C string which call Python */
- char *input = (char*)malloc(sizeof(100)*sizeof(char));
- /* a C string which will store the results */
- char *input2 = (char*)malloc(sizeof(100)*sizeof(char));
- FILE *f= (FILE*)fopen("test2.py","w");
- fprintf(f, prg);
- fclose(f);
- sprintf(input,"python test2.py %d %d",number1,number2);
- f = popen(input,"r");
- fgets(input2,100,f);
- fclose(f);
- number3 = atoi(input2);
- printf("%d\n",number3+10);
Let's assume that the program was saved as test2.c, then you would compile it the following way:
gcc test2.c
Then you would run it like this:
./a.out
Embedded Approach
What if you device didn't have an operating system? Or maybe you don't want your program to interfere with your operating system? One possibility is to embed python into your C program. Now, what does "embed" actually mean? Well, it means, make the interpreter part of your C program
First, what kind of system are you on? (you probably would stop somewhere in the last section in Windows, because I don't think popen is called popen in the Windows api).
Let's assume that you are using Ubuntu or something similar. First, we need to Figure out the flags for compiling and embedding Python into your C program. Where are the include files, and where are the library file?
I start by figuring out what kind of Python I run on my system
python --version
Which returns :
Python 2.6.5
Now I want to figure out where the .h file is, so I type:
locate Python.h
which returns :
/usr/include/python2.6/Python.h
Then I want to find the library file(which basically is the python interpreter):
locate libpython2.6.a
/usr/lib/python2.6/config/libpython2.6.a
Then I got all the things I need, now, let's make sure that we get this works before I do anything else.
We start by making an empty C program
Expand|Select|Wrap|Line Numbers
- #include <Python.h>
- #include <stdio.h>
- int main(){
- }
gcc -I/usr/include/python2.6/ -L/usr/lib/python2.6/config/ -lpython2.6 test3.c
Did it work? If yes, then let's continue to make an interesting C with Python program in test3.c. The program should do what it did earlier; we have two numbers (number1 and number2), we want python to add these two numbers, then we want to get the result of the addition in Python and place it in a variable called number3 in C
Expand|Select|Wrap|Line Numbers
- #include <stdio.h>
- #include <Python.h>
- int main(){
- int number1 = 10;
- int number2 = 32;
- Py_Initialize();
- char *input = (char*)malloc(sizeof(100)*sizeof(char));
- sprintf(input,"number3 = %d + %d",number1,number2);
- PyRun_SimpleString(input);
- PyObject * module = PyImport_AddModule("__main__");
- PyObject * dictionary = PyModule_GetDict(module);
- PyObject * result = PyDict_GetItemString(dictionary, "number3");
- long number3 = PyInt_AS_LONG(result);
- printf("%d\n",(int)number3);
- Py_Finalize();
- }
./a.out
In this case, we do not need to call the Python as a separate process, because Python is already part of your C program. Now which one should you use? The cons of the embedded version you have to bare careful when you compile your C program. The pros is that you don't interfere with the processes on your computer, write files to your harddrive etc.