0

I am trying to develop c extensions for python, which requires me to use

#include "Python.h"

in the c source code. However, it appears that Python.h in Ubuntu systems can be obtained by downloading python-dev, but this is not an option for macOS.

I tried compiling using /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/Headers/ (which contains a Python.h file) but When I use the command

gcc test.c -I /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/Headers/

I get the following error:

Undefined symbols for architecture arm64:
  "_PyObject_Size", referenced from:
      _main in test-165478.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Which makes it seem that the Python.h file on my mac is incomplete somehow. Is the solution to this problem downloading the source of python-dev and then using the Python.h header file that is in that package? If so, where could I find the python-dev source code?

  • 1
    The `Python.h` header is complete and working just fine. The problem is that you not linking in the definitions for the symbols that the header declares. Usually they're in a static library named `libpython.a`. – Brian61354270 Feb 07 '23 at 21:51
  • See also [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/q/12573816/11082165), in particular the "Failure to link against appropriate libraries/object files or compile implementation files" answer. – Brian61354270 Feb 07 '23 at 21:53

0 Answers0