About the source code.
I will try to embed python in C application. I will use a simple python script.
This is a simple example using pygame python module.
1 2 3 4 5 6 | import pygame screen = pygame.display.set_mode((640,400),0,32) BKColor = (255,255,255) screen.fill(BKColor) pygame.display.update() pygame.time.delay(1000) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | $ python Python 2.7.2 (default, Oct 27 2011, 01:36:46) [GCC 4.6.1 20111003 (Red Hat 4.6.1-10)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import pygame >>> screen = pygame.display.set_mode((640,400),0,32) >>> BKColor = (255,255,255) >>> screen.fill(BKColor) <rect(0, 0, 640, 400)> >>> pygame.display.update() >>> pygame.time.delay(1000) 1003 >>> |
Make a new folder and named Python_C using these commands in the shell:
1 2 3 | $ mkdir Python_C $ cd Python_C/ Python_C]$ ls |
Let’s see the C source code.
1 2 3 4 5 6 7 8 9 10 11 12 13 | #include int main() { printf("embed-simple-example\n"); Py_Initialize(); PyRun_SimpleString("import pygame"); PyRun_SimpleString("screen = pygame.display.set_mode((640,400),0,32)"); PyRun_SimpleString("BKColor = (255,255,255)"); PyRun_SimpleString("screen.fill(BKColor)"); PyRun_SimpleString("pygame.display.update()"); PyRun_SimpleString("pygame.time.delay(1000)"); return 0; } |
You can read more about functions and docs here.
I named the file test001.c.
About compiling.
I saw many topics where the people tell us about the solution to compile is this:
1 | $ g++ -o test001 test001.c -L/usr/lib/python2.7/config/ -lpython2.7 |
You can get this error:
1 | fatal error: Python.h: No such file or directory |
My opinion is not the correct way. See below my solution.
First, we use this:
1 | $ python-config --cflags |
1 | $ python-config --ldflags |
You will have two output and you will use them. In my case is:
1 2 3 4 5 | $ gcc -o test001 test001.c -I/usr/include/python2.7 -I/usr/include/python2.7 -fno-strict-aliasing -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m32 -march=i686 -mtune=atom -fasynchronous-unwind-tables -D_GNU_SOURCE -fPIC -fwrapv -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m32 -march=i686 -mtune=atom -fasynchronous-unwind-tables -D_GNU_SOURCE -fPIC -fwrapv -lpthread -ldl -lutil -lm -lpython2.7 -Xlinker -export-dynamic |
The result is test001. I run the application with:
1 2 3 | Python_C]$ ./test001 embed-simple-example |
… and is working well, see the screenshot below:
About errors.
First, the C source code can give some errors:
1 | warning: control reaches end of non-void function [-Wreturn-type] |
This error is because you not use return 0;
Another error is this:
1 | warning: return type defaults to ‘int’ [-Wreturn-type] |
If you don’t use this int main() to return type of main function.
This warning error is enabled by -Wall compile option.
That was, for now, I will return with tutorials on the subject.