DLunin
DLunin

Reputation: 1070

How to properly include libraries in a CMake project

For example, I have a project that looks like this:

project/
  subproject/
     CMakeLists.txt
     file1.h

  CMakeLists.txt
  main.cpp

project/CMakeLists.txt

project(some_project)
add_subdirectory(subproject)
set(SOURCE_FILES main.cpp)
add_executable(main ${SOURCE_FILES})
target_link_libraries(main subproject)

project/subproject/CMakeLists.txt:

project(subproject)
add_library(subproject_lib INTERFACE)
target_sources(subproject_lib INTERFACE file1.h)
target_include_directories(subproject_lib INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})

project/main.cpp

#include <file1.h>

// .. do something

Now, when I try to build project, I get a No such file or directory error.

Another example would be when I try to include dlib. When I do something like

add_subdirectory(dlib-19.9)
target_link_libraries(main dlib::dlib)

Which seems to be exactly what is in the dlib's own examples CMakeList.txt, I get an error.

So, how to include libraries in CMake the right way?

Upvotes: 0

Views: 1183

Answers (1)

piwi
piwi

Reputation: 5336

Your library is named subproject_lib, but you link main with subproject. I don't know why, but for some reason CMake does not complain if the target does not exist.

Upvotes: 2

Related Questions