'How library linking and header inclusion works with CMAKE

I am newbie to the CMAKE. please forgive me if you found my question silly.

I have following scenario in my project.

I have to use library libraryone in my librarytwo

may CMakeLists.txt for librarytwo looks like

cmake_minimum_required(VERSION 3.5)
find_package(libraryone REQUIRED)
add_library(librarytwo STATIC librarytwo.cpp)
target_link_libraries(librarytwo PUBLIC lib1::libraryone)

And my librarytwo.h and librarytwo.cpp looks like

librarytwo.h

#include <src/lib1/libraryone.h>
--------
-------

librarytwo.cpp

#include <librarytwo.h>
---------
--------

My libray two getting build properly.

Now I want to use librarytwo in librarythree

So my CMakeList.txt for librarythree looks like

cmake_minimum_required(VERSION 3.5)
add_library(librarythree STATIC librarythree.cpp)
target_include_directories(librarythree PRIVATE ${CMAKE_SOURECE_DIR}/src/lib2}
target_link_libraries(librarythree PRIVATE librarytwo)

my librarythree.cpp and libraythree.h looks like

librarythree.h

#include <src/lib2/librarytwo.h>
--------
-------

librarythree.cpp

#include <librarythree.h>
---------
--------

But while building librarythree, I am getting error in librarythree.h saying libraryone.h : No such file or directory

So, it was working for librarytwo.h and I have linked librarytwo to librarythree. So what I am missing ? help appreciated.



Solution 1:[1]

This is happening because you're including librarytwo.h, which in turn includes libraryone.h. You need to add a target_include_directories statement in your library 3 that tells it where to find libraryone.h.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jan