-
C Extensions for Python
In other words, we can write python methods in pure C.
-
Embedding Python a
The reasons for doing #1 are obvious because we can make speed improvements. For #2, it is due to transporting Python libraries over to C.
Compilation is a bit more challenging because we have to do something like:
gcc -I /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.7/include/python3.7m hello.c -o hello
However, we can actually utilize a hidden utility python seems to be hiding from us called "python-config."
As long as you can find where Python is installed, you can find the necessary directories.
With python-config
, you can do this instead:
# Double dollar sign is used to differentiate syntax usage if you use this in a Makefile
gcc $$(/usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.7/bin/python3.7-config --cflags) hello.c -o hello
#./hello
The embedded version requires further options so we have to add --embed
and --ldflags
to make things easier.
gcc $$(/opt/homebrew/Cellar/[email protected]/3.8.15/Frameworks/Python.framework/Versions/3.8/bin/python3.8-config --cflags --ldflags --embed) embedded.c -o embedded
./embedded