Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • mpcdf/training/cmake-recipes
  • seibl/cmake-recipes
2 results
Show changes
Commits on Source (2)
......@@ -70,6 +70,10 @@ check-recipes:
- cmake --build 10_production
- ./10_production/app
- CXXFLAGS="-march=icelake-server" cmake -S 11_customization -B 11_build
- cmake --build 11_build --verbose
- ./11_build/hello_world
check-links:
before_script:
- module purge
......
cmake_minimum_required(VERSION 3.20)
project(11_customization
LANGUAGES CXX)
##########################
# EXECUTABLE
##########################
add_executable(hello_world)
target_sources(hello_world PRIVATE main.cpp)
# How can I influence the build process as a user?
There are situations where you as a user need to influence the build process.
This can be adding custom compiler flags, include directories, linker flags, etc.
**If you are the library author you should consider using CMake for doing these thing.
The following is mainly intended for the end-user.**
## Automatic flags added by `CMAKE_BUILD_TYPE`
Targets provide modularization and encapsulation. A target
encapsulates everything needed for a certain build job. It also
is some kind of container that allows efficient use as a building
block.
| CMAKE_BUILD_TYPE | Compiler Flags | Linker Flags |
|------------------|------------------------------------------|------------------|
| Debug | `-g` | |
| Release | `-O3 -DNDEBUG` | |
| RelWithDebInfo | `-O2 -g -DNDEBUG` | |
| MinSizeRel | `-Os -DNDEBUG` | |
There is no default build type! If you do not specify a build types no flags will be added.
## CMake uses common environment variables to modify the compiler and linker commands.
CMake uses environment variables like `CFLAGS`, `CXXFLAGS`, `LDFLAGS`, ... to initialize its internal variables [[Ref]](https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_FLAGS.html#variable:CMAKE_%3CLANG%3E_FLAGS). You can either use the environment variables or the CMake variables directly.
**CAUTION! the internal variables are set during the first configure call and are cached. Meaning, subsequent changes to the environment variables will not be followed by CMake. To make CMake aware of the changes you can either delete and recreate the build folder or use `--fresh`[[Ref]](https://cmake.org/cmake/help/latest/manual/cmake.1.html#cmdoption-cmake-fresh).**
## Example
The following example shows how to add architecture specific optimization flags. **Note, without `CMAKE_BUILD_TYPE` no optimization level is set.**
```
CXXFLAGS="-march=icelake-server" cmake -S . -B build
cmake --build build --verbose
./build/hallo_world
```
\ No newline at end of file
#include <iostream>
int main(int /*argc*/, char** /*argv*/)
{
std::cout << "Hello World" << std::endl;
exit(EXIT_SUCCESS);
}
......@@ -20,6 +20,7 @@ we run them in our CI.
* [How to manage all the tests in your project?](08_ctest)
* [How to install a project with CMake?](09_install)
* [How to add compilation configurations to your project?](10_presets)
* [How can I influence the build process as a user (custom flags, etc)?](11_customization)
## Why CMake?
* Automatically search for programs, libraries, and header files
......