Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Organizing Sources in Subdirectories | Project Structure and Organization
Quizzes & Challenges
Quizzes
Challenges
/
Introduction to CMake

bookOrganizing Sources in Subdirectories

As your project grows, placing all files in a single folder quickly becomes difficult to manage. Splitting code into subdirectories allows you to group related functionality together and keeps your project easy to navigate. CMake supports this by allowing each directory to have its own CMakeLists.txt file.

# MyProject/
# ├── CMakeLists.txt
# ├── src/
# │   ├── CMakeLists.txt
# │   └── main.c
# └── include/
#     ├── CMakeLists.txt
#     └── mylib.h

Each folder in this structure controls its own build configuration. The root project simply connects them.

cmake_minimum_required(VERSION 3.10)
project(MyProject)

add_subdirectory(include)
add_subdirectory(src)

When CMake runs, it enters the include directory first, processes its configuration, and then moves into src. This keeps your build logic modular and easier to update.

Note
Note

add_subdirectory does not compile files by itself. It instructs CMake to process another folder and include it in the project build system.

For bigger projects, code is often grouped by purpose using nested directories. Libraries, utilities, and the main application are kept separate.

# NestedProject/
# ├── CMakeLists.txt
# ├── lib/
# │   ├── CMakeLists.txt
# │   ├── math/
# │   │   ├── CMakeLists.txt
# │   │   ├── math.c
# │   │   └── math.h
# │   └── utils/
# │       ├── CMakeLists.txt
# │       ├── utils.c
# │       └── utils.h
# └── app/
#     ├── CMakeLists.txt
#     └── main.c

Each folder defines its own rules and dependencies. CMake brings everything together into a single build system while letting each part remain independent. This structure makes testing, reusing code, and scaling your project much easier.

cmake_minimum_required(VERSION 3.10)
project(NestedProject)

add_subdirectory(lib)
add_subdirectory(app)
question mark

What is the purpose of the add_subdirectory command in CMake?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 3. Kapittel 3

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

bookOrganizing Sources in Subdirectories

Sveip for å vise menyen

As your project grows, placing all files in a single folder quickly becomes difficult to manage. Splitting code into subdirectories allows you to group related functionality together and keeps your project easy to navigate. CMake supports this by allowing each directory to have its own CMakeLists.txt file.

# MyProject/
# ├── CMakeLists.txt
# ├── src/
# │   ├── CMakeLists.txt
# │   └── main.c
# └── include/
#     ├── CMakeLists.txt
#     └── mylib.h

Each folder in this structure controls its own build configuration. The root project simply connects them.

cmake_minimum_required(VERSION 3.10)
project(MyProject)

add_subdirectory(include)
add_subdirectory(src)

When CMake runs, it enters the include directory first, processes its configuration, and then moves into src. This keeps your build logic modular and easier to update.

Note
Note

add_subdirectory does not compile files by itself. It instructs CMake to process another folder and include it in the project build system.

For bigger projects, code is often grouped by purpose using nested directories. Libraries, utilities, and the main application are kept separate.

# NestedProject/
# ├── CMakeLists.txt
# ├── lib/
# │   ├── CMakeLists.txt
# │   ├── math/
# │   │   ├── CMakeLists.txt
# │   │   ├── math.c
# │   │   └── math.h
# │   └── utils/
# │       ├── CMakeLists.txt
# │       ├── utils.c
# │       └── utils.h
# └── app/
#     ├── CMakeLists.txt
#     └── main.c

Each folder defines its own rules and dependencies. CMake brings everything together into a single build system while letting each part remain independent. This structure makes testing, reusing code, and scaling your project much easier.

cmake_minimum_required(VERSION 3.10)
project(NestedProject)

add_subdirectory(lib)
add_subdirectory(app)
question mark

What is the purpose of the add_subdirectory command in CMake?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 3. Kapittel 3
some-alt