Now that I’ve shown you the somewhat longer story, let’s fill in the rest. The next few sections go into more detail on compilation and linking, code structure, data conversions, error handling, and reference counts. These are core ideas in Python C extensions—some of which we will later learn you can often largely forget.
You always must compile C extension files such as the hello.c example and somehow link them with the Python interpreter to make them accessible to Python scripts, but there is wide variability on how you might go about doing so. For example, a rule of the following form could be used to compile this C file on Linux too:
hello.so: hello.c gcc hello.c -c -g -fpic -I$(PYINC) -o hello.o gcc -shared hello.o -o hello.so rm -f hello.o
To compile the C file into a shareable object file on Solaris, you might instead say something like this:
hello.so: hello.c cc hello.c -c -KPIC -o hello.o ld -G hello.o -o hello.so rm hello.o
On other platforms, it’s more different still. Because compiler options vary widely, you’ll want to consult your C or C++ compiler’s documentation or Python’s extension manuals for platform- and compiler-specific details. The point is to determine how to compile a C source file into your platform’s notion of a shareable or dynamically loaded object file. Once you have, the rest is easy; Python supports dynamic loading of C extensions on all major platforms today.
Because build details vary so widely from ...