Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Configuring and Generating Build Files | Creating and Building Projects
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

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 2

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

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

Suggested prompts:

What is the difference between in-source and out-of-source builds?

How do I set a custom output directory for my executable in CMake?

Can you explain the steps to configure and build a project using CMake?

bookConfiguring and Generating Build Files

Pyyhkäise näyttääksesi valikon

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

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 2
some-alt