Missing Python.h while trying to compile a C extension module

前端 未结 3 1402
灰色年华
灰色年华 2020-12-10 05:17

I\'m following this tutorial on how to extend Python with C\\C++ code.

The section named \"Building the extension module with GCC for Microsoft Windows\" fails for m

相关标签:
3条回答
  • 2020-12-10 05:39

    The Python official documentation has already made it clear. Check it out here

    The header files are typically installed with Python. On Unix, these are located in the directories prefix/include/pythonversion/ and exec_prefix/include/pythonversion/, where prefix and exec_prefix are defined by the corresponding parameters to Python’s configure script and version is '%d.%d' % sys.version_info[:2]. On Windows, the headers are installed in prefix/include, where prefix is the installation directory specified to the installer.

    To include the headers, place both directories (if different) on your compiler’s search path for includes. Do not place the parent directories on the search path and then use #include ; this will break on multi-platform builds since the platform independent headers under prefix include the platform specific headers from exec_prefix.

    And they have provided a convenient way to get the correct cflags that we should pass to compiler. here

    So for example, here is what I got after running the command

    root@36fd2072c90a:/# /usr/bin/python3-config --cflags
    -I/usr/include/python3.5m -I/usr/include/python3.5m  -Wno-unused-result -Wsign-compare -g -fstack-protector-strong -Wformat -Werror=format-security  -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes
    

    Pass those flags to the compiler, and it will work.

    0 讨论(0)
  • 2020-12-10 05:53
    1. Do you have the python dev files so that you can find Python.h?
    2. Do you have the location of Python.h specified to your compiler? with gcc this is usually done through a -I path to include.

    Figuring out which of those is failing will solve your problem.

    from the article you linked:

    gcc -c hellomodule.c -I/PythonXY/include

    gcc -shared hellomodule.o -L/PythonXY/libs -lpythonXY -o hello.dll

    They assumed you installed python in the default location c:\pythonXY(Where X is the major version number and Y is the minor version number).(in your case Python26) If you put python somewhere else replace /PythonXY with where ever you installed it.

    0 讨论(0)
  • 2020-12-10 05:54

    For Linux, Ubuntu users to resolve the issue of missing Python.h while compiling, simply run the following command in your terminal to install the development package of python:

    In Terminal: sudo apt-get install python-dev

    Good luck

    0 讨论(0)
提交回复
热议问题