Skip to content

Add an include folder

Sometimes you want to add an arbitrary folder to your projects include path. The simplest way to do this is with the include_directories command.

cmake
cmake_minimum_required(VERSION 3.28)
project(my-project)
include_directories(include)
add_executable(my-app src/main.c src/greet.c)
c
#include <greet.h>
int main() {
    greet("George Washington");
    return 0;
}
c
#pragma once
void greet(char *name);
c
#include <greet.h>
#include <stdio.h>
void greet(char *name) { printf("Hello %s!\n", name); }
sh
cmake -B build
cmake --build build
./build/my-app

🆕 The better way

The include_directories() command adds the specified directories to the include path of all targets in the current directory scope. This is fine for small projects, but it can become unwieldy as your project grows. The target_include_directories() command is a more granular alternative that allows you to specify the include path for a single target.

For example, this CMakeLists.txt file is equivalent to the one above:

cmake
cmake_minimum_required(VERSION 3.28)
project(my-project)
add_executable(my-app src/main.c src/greet.c)
target_include_directories(my-app PRIVATE include)

ℹ Notice how we use the target name that was declared & defined by add_executable() again in target_include_directories() to only affect that executable target. You must put any add_executable() or add_library() declarations before target_include_directories() so that the target is defined by the time the interpreter gets to the target_include_directories() function.

What's the PRIVATE argument for?

It's a required argument. The enum options are PUBLIC, PRIVATE, and INTERFACE.

The INTERFACE, PUBLIC and PRIVATE keywords are required to specify the scope of the following arguments. PRIVATE and PUBLIC items will populate the INCLUDE_DIRECTORIES property of <target>. PUBLIC and INTERFACE items will populate the INTERFACE_INCLUDE_DIRECTORIES property of <target>. The following arguments specify include directories.

📚 CMake documentation on include_directories()
📚 CMake documentation on target_include_directories()