Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Organizing Sources in Subdirectories | Project Structure and Organization
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

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 3

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

bookOrganizing Sources in Subdirectories

Deslize para mostrar o menu

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

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 3
some-alt