Decrease the length of headers with help of target_include_directories

Viewed 82

I have a project

├── CMakeLists.txt
│   ├── log
│   │   ├── CMakeLists.txt
│   │   ├── include
│   │   │   ├── log.h
│   │   └── src
│   │       ├── log.cpp
│   └── main.cpp

In log.cpp I am usng #include "../include/log.h" and in main.cpp I amd using #include "include/log.h"

I want to use #include "log.h"

I read that target_include_directories can help me.

How can I apply it to my CMakeLists.txt

cmake_minimum_required(VERSION 3.18)

project(Logger)

# specify the C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)

#include_directories(log/include/) -- I used this, but I want to use target_include_directories

add_library(log_lib log/src/log.cpp)

add_executable(demo main.cpp)
target_link_libraries(demo log_lib)
2 Answers

define the target and then use the name as the first argument for target_include_directories

add_library(log_lib log/src/log.cpp)
target_include_directories(log_lib PUBLIC log/include)

worry about INTERFACE vs PUBLIC vs PRIVATE after you've got it all working and you want to understand it better. (This option transitively affects targets that depend on your library).

The line below helped

target_include_directories(log_lib PUBLIC log/include)
Related