0

I am having some trouble building a simple C++ project in Windows.

What I want to do:

  1. Run some simple code in C++
  2. Print a graph using the matplotlibcpp library.

I installed the matplotlibcpp library by using vcpkg. I have a problem with the matplotlibcpp.h header including Python.h.

I obtain this error: Error code 1083 - Cannot open include file: 'Python.h': No such file or directory.

I have anaconda installed on my PC and the path to Python is added to the environment PATH in Windows.

My code:

#include "matplotlibcpp.h"

using namespace std;
namespace plt = matplotlibcpp;

int main()
{
    cout << "Hello!" << endl;

    plt::plot({ 1,3,2,4 });
    plt::show();

    return 0;
}

My CMakeList.txt:

cmake_minimum_required (VERSION 3.8)

project ("MyProj" VERSION 1.0)

include_directories(include)


# Add source to this project's executable.
file(GLOB_RECURSE SOURCES "src/*.cpp")
add_executable (MyEx ${SOURCES} "main.cpp" "main.h")

find_path(MATPLOTLIB_CPP_INCLUDE_DIRS "matplotlibcpp.h"
            HINTS
            C:/Users/claudiop/dev/vcpkg/vcpkg/installed/x86-windows/include/matplotlibcpp.h)
   target_include_directories(MyEx PRIVATE ${MATPLOTLIB_CPP_INCLUDE_DIRS})

I tried to follow indications in this post can't include Python.h in visual studio but I did not manage to solve the problem.

Any suggestion/help/hint?

Blue Winter
  • 5
  • 1
  • 5
  • Using `target_include_directories` you adds directory, contained `matplotlibcpp.h` header, to the include directories list. You need to do the same for directory, contained `Python.h` header. "the path to Python is added to the environment PATH in Windows" - PATH variable is for search executables and libraries. It doesn't affect on searching for header files. Note, that aside adding include directories when compile your executable, you need to link the executable with appropriate libraries. – Tsyvarev May 21 '21 at 16:32
  • Thanks a lot for the answer. Adding ```find_path(PYTHON_INCLUDE_DIRS "Python.h" HINTS C:/Users/claudiop/AppData/Local/Continuum/anaconda3/pkgs/python-3.7.4-h5263a28_0/include/Python.h) target_include_directories(MyEx PRIVATE ${PYTHON_INCLUDE_DIRS}) ``` I still obtain the same error – Blue Winter May 21 '21 at 17:20
  • Ask Visual Studio to show exact command line which are executed for building the project: https://stackoverflow.com/questions/6373841/can-i-view-what-build-commands-are-actually-executed-with-msbuild/6373934. Check, that command line actually contains include option with a directory, which in turn contains `Python.h` header. – Tsyvarev May 21 '21 at 17:25
  • This is the error from the Output window with Detailed option on: 1>------ Build started: Project: ZERO_CHECK, Configuration: Debug|x64 ------ 2>------ Build started: Project: AUF, Configuration: Debug|x64 ------ 2> main.cpp 2>C:\Users\claudiop\dev\vcpkg\vcpkg\packages\matplotlib-cpp_x86-windows\include\matplotlibcpp.h(5,10): fatal error C1083: Cannot open include file: 'Python.h': No such file or directory ========== Build: 1 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== – Blue Winter May 21 '21 at 17:32
  • Hm, this is not a command line used for compile `main.cpp`. Probably, it should be other option for showing this command line. But I don't know how to make this in Visual Studio. – Tsyvarev May 21 '21 at 17:40
  • Maybe I misunderstood, the build output in the command line that you wanted to look at maybe is this one: Microsoft (R) Build Engine version 16.9.0+5e4b48a27 for .NET Framework Copyright (C) Microsoft Corporation. All rights reserved. AUV.cpp write_csv.cpp main.cpp C:\Users\claudiop\source\AUF\main.cpp(8,10): fatal error C1083: Cannot open include file: 'matplotlibcpp.h': No such file or directory [C:\Users\claudiop\source\AUF\out\build\MyEx.vcxproj] Generating Code... – Blue Winter May 21 '21 at 17:44
  • No, this is not a command line. But that error message is about not finding `matplotlibcpp.h` header itself, not about `Python.h`. So, what is the **actual** error message corresponded to **your code**? – Tsyvarev May 21 '21 at 17:51

1 Answers1

0

Python.h is a header of a library that responsible for embeding Python code into CPP. You probably don't have the library itself installed thus the error. You probably can do so using vcpkg. If you do have it and still suffer experience the issue then the text bellow should give you an idea of how things work. Unfortunately that's all I can do for you because I don't have much experience with Windows.

Python.h is a header of shared library. You'll need to specify a path to .so (I believe this format is called .dll on Windows) object related to this library. Here is how to do it on Linux:

set(PYTHON_HEADERS /usr/include/python3.9 ) # path to header
set(PYTHON_STATIC  /usr/lib64             ) # path to where windows stores dlls

add_library(python_embeded SHARED IMPORTED GLOBAL)
set_target_properties(python_embeded PROPERTIES
    IMPORTED_LOCATION ${PYTHON_STATIC}/libpython3.9.so
    INTERFACE_INCLUDE_DIRECTORIES ${PYTHON_HEADERS}
    LINKER_LANGUAGE C
    )
    
        add_executable(program main.cpp)
 set_target_properties(program PROPERTIES
    LINKER_LANGUAGE CXX
    )
    
target_link_libraries(program PRIVATE python_embeded)

I have no idea how it's done on Windows so I'll give an idea of how it works on Linux. Linux puts all headers that you include with <> into special directory which is /usr/include. On Windows you'll probably have to specify where is the header you are trying to include or outright put it into your project directory. You'll need to do the same with .dll files.

trofchik
  • 87
  • 1
  • 7