Search code examples
c++cmakeincludesubdirectory

CMake header file that includes header files from other directory


Given this tree:

project
 + lib
    + include
        + include_both.h
    + source
        + file1.cpp
        + file1.h
        + file2.cpp
        + file2.h
 + main_dir
    + main.cpp

After linking lib to main_dir with CMake, I would like header files file1.h and file2.h to not to be visible from main.cpp. I want the library to be include-able only by the include_both.h header file. How can this be done and should I even do it?


Solution

  • You might try making the include directory a PUBLIC one, so it is visible to consumers of the library, but keep the source directory private:

    add_library(MyLib SHARED 
        source/file1.cpp
        source/file2.cpp
    )
    
    target_include_directories(MyLib 
        PUBLIC  include
        PRIVATE source
    )