Header file of package not found.

On windows 10 (MSVC build toolset without Visual Studio), I’m trying to build my C++ project, which uses one package (for plotting): Matplot++. I cloned it’s repo in my project’s folder.

In my project’s main.cpp, I’m able to #include <matplot/matplot.h> and plot things. But when trying to do the same in a module (knn), this include directive results in an error during knn.cpp compilation (using vcpkg leads to the same result):

[project folder]\knn\knn.cpp(1,10): fatal  error 
C1083: Cannot open include file: 'matplot/matplot.h': No such file or
directory [[project folder]\build\knn\knn.vcxproj]

My project’s CMakeLists.txt looks like this:

cmake_minimum_required(VERSION 3.5.0)
project(ml_cpp VERSION 0.1.0 LANGUAGES C CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_subdirectory(matplotplusplus)

add_subdirectory(knn)
add_subdirectory(kmeans)
add_subdirectory(lib)

add_executable(mlCPP src/main.cpp)

target_link_libraries(mlCPP PUBLIC knn kmeans lib)
target_link_libraries(mlCPP PRIVATE Matplot++::matplot)

target_include_directories(mlCPP PUBLIC
  "${CMAKE_HOME_DIRECTORY}/include"
  "${CMAKE_HOME_DIRECTORY}/knn/include"
  "${CMAKE_HOME_DIRECTORY}/kmeans/include"
)

And the project folder contains the following folders and files:

  • build
  • include: coheir.h, data_handler.h, data.h
  • kmeans: CMakeLists.txt, kmeans.cpp, kmeans.h
  • knn: CMakeLists.txt, knn.cpp, knn.h
  • lib: CMakeLists.txt, coheir.cpp, data_handler.cpp, data.cpp
  • matplotplusplus
  • src: main.cpp
  • CMakeLists.txt

What is the problem? How do I fix this?

If that “knn” is also a target (seems to be a library), then it would also need target_include_directories() with Matplot headers (or just linking to Matplot library, since that seems to be enough for “mlCPP” target).

You are right! target_include_directories() didn’t solve it (at least not how I used it). But knn is a library. This module’s CMakeLists.txt only included add_library(knn ./knn.cpp) until now. And adding a second line with target_link_libraries(knn PUBLIC Matplot++::matplot) solved my problem.

I was stuck in this problem for days… thank you so much!

1 Like