Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Configuring and Generating Build Files | Creating and Building Projects
Quizzes & Challenges
Quizzes
Challenges
/
Introduction to CMake

bookConfiguring and Generating Build Files

Configuring and Generating Build Files

When working with CMake, you need to configure your project before building it. This step generates platform-specific build files based on your CMakeLists.txt configuration.

Before running CMake, your project directory usually looks like this:

my_project/
├── CMakeLists.txt
└── main.c

In-source vs Out-of-source Builds

CMake allows two ways to generate build files:

  • In-source build: Build files are generated directly inside your project directory. This can clutter your source folder with generated files such as CMakeFiles/ and CMakeCache.txt.

  • Out-of-source build: Build files are generated in a separate directory (commonly named build). This keeps your source directory clean and makes cleanup easier.

Out-of-source builds are the recommended approach because you can remove all generated files simply by deleting the build folder.

Custom Output Directory Example

You can also control where your compiled program is placed. In the example below, the executable is stored inside a bin directory in the build folder:

main.c

main.c

CMakeLists.txt

CMakeLists.txt

copy
123456
#include <stdio.h> int main() { printf("Hello from custom output directory!\n"); return 0; }

This ensures that build results are well organized and do not mix with your source files.

Configuring and Building the Project

To generate build files and compile your project, run the following commands:

mkdir build
cd build
cmake ..
cmake --build .

If a custom output directory is set, the executable will appear in the location you configured.

Note
Note

Out-of-source builds are recommended because they keep your source directory clean and allow you to remove all build files by deleting a single folder. This reduces the risk of accidental file deletion and simplifies maintenance.

question mark

Why are out-of-source builds generally preferred when using CMake?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 2

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

bookConfiguring and Generating Build Files

Scorri per mostrare il menu

Configuring and Generating Build Files

When working with CMake, you need to configure your project before building it. This step generates platform-specific build files based on your CMakeLists.txt configuration.

Before running CMake, your project directory usually looks like this:

my_project/
├── CMakeLists.txt
└── main.c

In-source vs Out-of-source Builds

CMake allows two ways to generate build files:

  • In-source build: Build files are generated directly inside your project directory. This can clutter your source folder with generated files such as CMakeFiles/ and CMakeCache.txt.

  • Out-of-source build: Build files are generated in a separate directory (commonly named build). This keeps your source directory clean and makes cleanup easier.

Out-of-source builds are the recommended approach because you can remove all generated files simply by deleting the build folder.

Custom Output Directory Example

You can also control where your compiled program is placed. In the example below, the executable is stored inside a bin directory in the build folder:

main.c

main.c

CMakeLists.txt

CMakeLists.txt

copy
123456
#include <stdio.h> int main() { printf("Hello from custom output directory!\n"); return 0; }

This ensures that build results are well organized and do not mix with your source files.

Configuring and Building the Project

To generate build files and compile your project, run the following commands:

mkdir build
cd build
cmake ..
cmake --build .

If a custom output directory is set, the executable will appear in the location you configured.

Note
Note

Out-of-source builds are recommended because they keep your source directory clean and allow you to remove all build files by deleting a single folder. This reduces the risk of accidental file deletion and simplifies maintenance.

question mark

Why are out-of-source builds generally preferred when using CMake?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 2
some-alt