Python.h: No such file or directory

后端 未结 6 1929
误落风尘
误落风尘 2020-12-23 13:12

I recently installed KDevelop 4 for C++ development on my Macbook Pro running Ubuntu 12.04 LTS.

I want to embed Python application in my C++ code. To do that, one ne

相关标签:
6条回答
  • 2020-12-23 13:28

    You want to include the following on the compile line:

    `python-config --cflags`
    

    and this on the link line:

    `python-config --ldflags`
    
    0 讨论(0)
  • 2020-12-23 13:37

    In your CMakeLists.txt, try adding the following:

    find_package(PythonLibs REQUIRED)
    include_directories(${PYTHON_INCLUDE_DIRS})
    target_link_libraries(<your exe or lib> ${PYTHON_LIBRARIES})
    

    For details of the commands, run:

    cmake --help-module FindPythonLibs
    cmake --help-command find_package
    cmake --help-command include_directories
    cmake --help-command target_link_libraries
    
    0 讨论(0)
  • 2020-12-23 13:46

    I assume that it is already installed. Find the path with:

    find / -iname python.h
    

    and when you have done so, when compiling add

    -I python_h_path
    
    0 讨论(0)
  • 2020-12-23 13:52

    Most likely Python.h is not in your build systems' include path. You can find out where your Python.h is by running

    dpkg -L python-dev | grep Python.h
    

    This will also verify that the python-dev package actually installed a Python.h.

    I don't have a kdevelop here, but most IDEs have a setting somewhere where you can specify the include path used by the build system, and you should be able to add the path where Python.h lies there.

    EDIT:

    As Nikolai implied, you will also need to add the correct library path for the linking stage. (Output of python-config --ldflags).

    0 讨论(0)
  • 2020-12-23 13:55
    sudo apt-get install pythonX.X-dev
    

    For example for 3.8

    sudo apt-get install python3.8-dev
    

    Thank you Cristianjs19 for the comment.

    Original Answer:

    sudo apt-get install python2.7-dev
    

    worked for me on a "Python.h: No such file or directory" issue

    0 讨论(0)
  • 2020-12-23 13:55

    For Linux Ubuntu Putty Users try this:

    sudo apt-get update
    sudo apt-get install python-dev
    

    then compile it

    gcc -o check xyz.c -I/usr/include/python2.7/ -lpython2.7
    

    then run it

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