How do I actually set up an environment for writing C extensions?

The documentation for the C API seems to skip a step; it launches directly into an explanation of coding standards, the standard headers and what they provide, etc. The “Extending Python with C or C++¶” (why is this separated?) section, similarly, appears to start and end with code examples. It doesn’t explain the necessary setup steps required before compilation is possible.

I assumed it wouldn’t be automatic simply because I have Python and gcc on my system, but just to verify:

$ gcc empty.c 
empty.c:2:10: fatal error: Python.h: No such file or directory
    2 | #include <Python.h>
      |          ^~~~~~~~~~
compilation terminated.

Where exactly are the header files supposed to be; how am I intended to obtain them (only as part of the entire repository? It’s not my intent to fork Python here yet); and how is the compiler intended to know where to look for them?

1 Like

You might need to separately install the Python development package for your system. On Debian, I can apt install python3 to have the Python runtime, but to build extensions, apt install python3-dev is needed, to get all of those headers.

The other possibility is that you’d need to tell GCC where to find those headers. It’s common to use pkg-config for this purpose:

rosuav@sikorsky:~/tmp$ cat empty.c
#include <Python.h>

int main() {return 0;}

rosuav@sikorsky:~/tmp$ pkg-config --cflags --libs python3
-I/usr/local/include/python3.12 -L/usr/local/lib
rosuav@sikorsky:~/tmp$ gcc empty.c
empty.c:1:10: fatal error: Python.h: No such file or directory
    1 | #include <Python.h>
      |          ^~~~~~~~~~
compilation terminated.
rosuav@sikorsky:~/tmp$ gcc empty.c `pkg-config --cflags --libs python3`
rosuav@sikorsky:~/tmp$ ./a.out

That might be all you need. Obviously the details will depend on your exact system, though.

Using the C API is hard work. There are libraries that make it easier.
You might like to use one of those libraries.
Personally I use PyCxx, but then I am the maintainer.
It is packaged for fedora and debian.

On Windows, the header files and static library files are in subfolders of the Python folder where python.exe lives, and they’re included by default, so it’s just a case of pointing to them when using Visual Studio.