Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Specifying Include Directories | Libraries, Includes, and Build Types
Quizzes & Challenges
Quizzes
Challenges
/
Introduction to CMake

bookSpecifying Include Directories

In real projects, header files are usually stored in a separate include/ directory. To allow the compiler to locate these headers, CMake must be told where to look. This is done using the target_include_directories command, which attaches include paths directly to a specific target.

CMakeLists.txt

CMakeLists.txt

copy
12345678
cmake_minimum_required(VERSION 3.10) project(SampleIncludeProject) add_library(mylib src/mylib.c) target_include_directories(mylib PUBLIC include) add_executable(main src/main.c) target_link_libraries(main PRIVATE mylib)

In this example, target_include_directories(mylib PUBLIC include) tells CMake to add the include/ directory to the compiler’s search path for the mylib library. Because PUBLIC is used, any executable that links to mylib also receives this include path automatically.

Note
Note

Include directories should be assigned to specific targets instead of being applied globally. This keeps your build configuration clean and avoids accidental dependencies.

Controlling Header Visibility

CMake allows you to control who can see which headers. This helps maintain clear boundaries between public APIs and internal implementation details.

CMakeLists.txt

CMakeLists.txt

copy
123456789101112
cmake_minimum_required(VERSION 3.10) project(IncludeVisibilityExample) add_library(core src/core.c) target_include_directories(core PUBLIC include/core PRIVATE src/private_headers INTERFACE include/interfaces ) add_executable(main src/main.c) target_link_libraries(main PRIVATE core)

Here, include/core is visible to both the core library and any program that links to it. The src/private_headers directory is used only inside the library and is hidden from other targets. The include/interfaces directory is not used by the library itself but becomes visible to anything that links against it.

This setup enforces encapsulation and prevents consumers of the library from relying on internal headers.

question mark

What does the PUBLIC keyword mean when used with target_include_directories?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 4. Luku 3

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

bookSpecifying Include Directories

Pyyhkäise näyttääksesi valikon

In real projects, header files are usually stored in a separate include/ directory. To allow the compiler to locate these headers, CMake must be told where to look. This is done using the target_include_directories command, which attaches include paths directly to a specific target.

CMakeLists.txt

CMakeLists.txt

copy
12345678
cmake_minimum_required(VERSION 3.10) project(SampleIncludeProject) add_library(mylib src/mylib.c) target_include_directories(mylib PUBLIC include) add_executable(main src/main.c) target_link_libraries(main PRIVATE mylib)

In this example, target_include_directories(mylib PUBLIC include) tells CMake to add the include/ directory to the compiler’s search path for the mylib library. Because PUBLIC is used, any executable that links to mylib also receives this include path automatically.

Note
Note

Include directories should be assigned to specific targets instead of being applied globally. This keeps your build configuration clean and avoids accidental dependencies.

Controlling Header Visibility

CMake allows you to control who can see which headers. This helps maintain clear boundaries between public APIs and internal implementation details.

CMakeLists.txt

CMakeLists.txt

copy
123456789101112
cmake_minimum_required(VERSION 3.10) project(IncludeVisibilityExample) add_library(core src/core.c) target_include_directories(core PUBLIC include/core PRIVATE src/private_headers INTERFACE include/interfaces ) add_executable(main src/main.c) target_link_libraries(main PRIVATE core)

Here, include/core is visible to both the core library and any program that links to it. The src/private_headers directory is used only inside the library and is hidden from other targets. The include/interfaces directory is not used by the library itself but becomes visible to anything that links against it.

This setup enforces encapsulation and prevents consumers of the library from relying on internal headers.

question mark

What does the PUBLIC keyword mean when used with target_include_directories?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 4. Luku 3
some-alt