Skip to content

Commit

Permalink
Extend CMake part with a few remarks
Browse files Browse the repository at this point in the history
  • Loading branch information
uekerman committed Nov 18, 2024
1 parent f581e05 commit 452e6d8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
15 changes: 11 additions & 4 deletions 03_building_and_packaging/cmake_demo.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,22 @@ Example code is in [`building-and-packaging/material/examples/cmake`](https://gi
## CMake "Hello World"

- Look at and explain `CMakeLists.txt`.
- `cmake --help-command-list`, `cmake --help-command add_executable`
- `mkdir build && cd build && cmake ..`
- Standard to create `build` directory, don't call `cmake` in root directory.
- In case of doubt, you can always just delete complete folder.
- And you can have multiple build folders.
- Explain and look at files:
- `Makefile`: lengthier than you think, many targets, search for `helloworld`
- `CMakeCache.txt`: stores values of variables, used by GUIs for example
- `cmake_install.cmake`: a file used by CPack internally, ignore it
- `CMakeFiles`: even much more things that are not so important right now
- `make` and `./helloworld`
- `make clean`
- Makefiles are created by default, one can also create other things: `cmake --help`
- A bit more modern: From project root call:
- `cmake -S. -Bbuild`
- `cmake --build build` (independent of generator)

## Multiple files

Expand Down Expand Up @@ -96,7 +102,7 @@ int main()
`main.cpp`:

```diff
+ #include "precice/SolverInterface.hpp"
+ #include "precice/precice.hpp"
...
sse();
+ std::cout << precice::getVersionInformation() << std::endl;
Expand All @@ -116,6 +122,7 @@ target_link_libraries(helloworld PRIVATE precice::precice)
- There is a ["module" and a "config" mode](https://cmake.org/cmake/help/latest/command/find_package.html)
- "module" mode
- Is there a `Findprecice.cmake` module in CMake? Some are shipped with CMake. But this doesn't scale.
- Find out which: `cmake --help-module-list | grep "Find"`
- Is there a `Findprecice.cmake` module elsewhere? We could ship one with our helloworld program. But these modules often out-of-date.
- "config" mode (here the case, the newer/better way of doing things)
- Is there a `preciceCongfig.cmake`? A file installed by preCICE. Scales and up-to-date.
Expand All @@ -135,8 +142,6 @@ target_link_libraries("${PROJECT_NAME}" PRIVATE precice)
- Use variables to talk to CMake.
- Let's try to make preCICE an optional dependency of our code (not really everybody has preCICE installed, sadly).

`CMakeLists.txt`:

`main.cpp`:

```c++
Expand All @@ -145,7 +150,9 @@ target_link_libraries("${PROJECT_NAME}" PRIVATE precice)
#endif
```

and around header.
and around header (double negating is also the standard way to do things here).

`CMakeLists.txt`:

```cmake
option(ENABLE_PRECICE "Enable use of preCICE." ON)
Expand Down
3 changes: 2 additions & 1 deletion 03_building_and_packaging/cmake_slides.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ slideOptions:

## Learning Goals of This Unit

- Understand the motivation of learning CMake.
- Explain the motivation of learning CMake.
- Work with CMake as a user of a project following good practices (using variables, specifying locations of dependencies).
- Develop simple CMake projects (executable vs. library, dependencies) and know how to learn more.

Expand Down Expand Up @@ -168,3 +168,4 @@ slideOptions:
- [An Introduction to Modern CMake](https://cliutils.gitlab.io/modern-cmake/)
- [Professional CMake](https://crascit.com/professional-cmake/), an up-to-date (non-public) book
- [CMake Cookbook](https://github.com/dev-cafe/cmake-cookbook)
- [Code Refinery CMake course](https://coderefinery.github.io/cmake-workshop/)

0 comments on commit 452e6d8

Please sign in to comment.