c++pythonincludecmakekdevelop

Python.h: No such file or directory


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 needs to include the Python.h header file. So, I did that.

#include <iostream>
#include <Python.h>

int main(int argc, char **argv) {
    Py_Initialize();
    return 0;
}

However, on running, I received the following response from the IDE:

fatal error: Python.h: No such file or directory

I have python-dev package installed.

So, I thought it must be an issue with the header file not being included by KDevelop. Thus, I added the relevant folder to the include path and KDevelop immediately recognized that by removing the red underline beneath the second include statement in the code above.

But still, the problem remains. I get the same error. Would appreciate any help or inputs you guys can provide :-)

(KDevelop is using CMake for my project.)


Solution

  • 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