Skip to content

Commit 07d6f71

Browse files
uekermanMakisH
andauthored
Update linux, make and cmake basics (#204)
Co-authored-by: Gerasimos Chourdakis <gerasimos.chourdakis@ipvs.uni-stuttgart.de>
1 parent 45075d2 commit 07d6f71

File tree

9 files changed

+47
-88
lines changed

9 files changed

+47
-88
lines changed

03_building_and_packaging/cmake_demo.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,22 @@ Example code is in [`building-and-packaging/material/examples/cmake`](https://gi
55
## CMake "Hello World"
66

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

1925
## Multiple files
2026

@@ -70,7 +76,7 @@ int main()
7076
- add_executable(helloworld "${SRC_FILES}")
7177
```
7278

73-
- Build static lib `libHelloWorld.a`, that's the default
79+
- Build static lib `libsse.a`, that's the default
7480
- Build shared lib by `cmake -DBUILD_SHARED_LIBS=ON ..`, don't change in `CMakeLists.txt`, but stick to standards
7581
- Define CMake variables with `-D`, we will come back to this later
7682
- Now, let's use the library:
@@ -96,7 +102,7 @@ int main()
96102
`main.cpp`:
97103

98104
```diff
99-
+ #include "precice/SolverInterface.hpp"
105+
+ #include "precice/Tooling.hpp"
100106
...
101107
sse();
102108
+ std::cout << precice::getVersionInformation() << std::endl;
@@ -107,15 +113,17 @@ sse();
107113
`CMakeLists.txt`:
108114

109115
```diff
110-
find_package(precice REQUIRED CONFIG)
116+
find_package(precice 3 REQUIRED CONFIG)
111117
target_link_libraries(helloworld PRIVATE precice::precice)
112118
```
113119

114120
- `cmake ..` and `make` and `./helloworld`
115121
- `find_package` works if dependency is "cmake-ready"
122+
- `3` is the major version we look for.
116123
- There is a ["module" and a "config" mode](https://cmake.org/cmake/help/latest/command/find_package.html)
117124
- "module" mode
118125
- Is there a `Findprecice.cmake` module in CMake? Some are shipped with CMake. But this doesn't scale.
126+
- Find out which: `cmake --help-module-list | grep "Find"`
119127
- Is there a `Findprecice.cmake` module elsewhere? We could ship one with our helloworld program. But these modules often out-of-date.
120128
- "config" mode (here the case, the newer/better way of doing things)
121129
- Is there a `preciceCongfig.cmake`? A file installed by preCICE. Scales and up-to-date.
@@ -135,8 +143,6 @@ target_link_libraries("${PROJECT_NAME}" PRIVATE precice)
135143
- Use variables to talk to CMake.
136144
- Let's try to make preCICE an optional dependency of our code (not really everybody has preCICE installed, sadly).
137145

138-
`CMakeLists.txt`:
139-
140146
`main.cpp`:
141147

142148
```c++
@@ -145,7 +151,9 @@ target_link_libraries("${PROJECT_NAME}" PRIVATE precice)
145151
#endif
146152
```
147153

148-
and around header.
154+
and around header (double negating is also the standard way to do things here).
155+
156+
`CMakeLists.txt`:
149157

150158
```cmake
151159
option(ENABLE_PRECICE "Enable use of preCICE." ON)

03_building_and_packaging/cmake_exercise.md

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,54 +4,40 @@ In this exercise, we need to fight. Not everything always works smoothly. This i
44

55
To get an independent and reproducible environment as common ground, we use and, thus repeat, Docker.
66

7-
Deadline: **Thursday, December 1st, 2022, 9:00**
7+
Deadline: **Thursday, November 27th, 2024, 9:00**
88

99
## Overview
1010

11-
- The goal of the exercise is to open a pull request from a fork of [the CMake exercise repository](https://github.com/Simulation-Software-Engineering/cmake-exercise-wt2223). Please name your pull request `Add building and container recipes` and assign yourself. In the pull request description, please explain what we need to do to test your code.
12-
- Your should add a `Dockerfile` and a `CMakeLists.txt`, besides some minor changes in `main.cpp` like commenting in some code parts. It should be possible to create an executable container from your pull request. Inside the container, one should be able to directly build the C++ code (`main.cpp`) using CMake. Use as many of the currently commented-out additional files, which induce additional dependencies.
11+
- The goal of the exercise is to open a pull request from a fork of [the CMake exercise repository](https://github.com/Simulation-Software-Engineering/cmake-exercise-wt2425). Please name your pull request `Add building and container recipes` and assign yourself.
12+
- Your pull request should add a `Dockerfile` and a `CMakeLists.txt`, besides some minor changes in `main.cpp`, such as commenting in some code parts.
13+
- From your pull request, it should be possible to create an executable container. After running the container, it should be possible to `cd` into `cmake-exercise` and then run the `build_and_run.sh` script.
14+
- Use as many of the currently commented-out additional files, which induce additional dependencies.
1315

1416
## First Steps
1517

1618
1. Fork and clone the repository, have a look at the `main.cpp` and the `README.md`.
1719
2. Build `main.cpp` manually (e.g. `g++ main.cpp -o main`) and run the executable (`./main`).
1820
3. Build a Docker image, run a container (in interactive mode), and repeat steps 1 and 2 within the container.
21+
4. Look into and use the `build_and_run.sh` script.
1922

2023
## Repository Structure
2124

2225
The bare `main.cpp` uses several additions, which are located in the following subdirectories, each containing a `cpp` and a `hpp` file with the same name.
2326

24-
- `flatset` adds a function to create and modify a flat set using [Boost Container](https://www.boost.org/doc/libs/1_80_0/doc/html/container.html) and outputs the set. This example is adapted from a [cppsecrets blog post](http://cppsecrets.com/article.php?id=2834).
25-
- `filesystem` adds a function to inspect and output the current directory using [Boost Filesystem](https://www.boost.org/doc/libs/1_75_0/libs/filesystem/doc/index.htm). This example is adapted from [tutorial 4](https://www.boost.org/doc/libs/1_80_0/libs/filesystem/example/tut4.cpp) of Boost Filesystem.
27+
- `flatset` adds a function to create and modify a flat set using [Boost Container](https://www.boost.org/doc/libs/1_86_0/doc/html/container.html) and outputs the set. This example is adapted from a [cppsecrets blog post](http://cppsecrets.com/article.php?id=2834).
28+
- `filesystem` adds a function to inspect and output the current directory using [Boost Filesystem](https://www.boost.org/doc/libs/1_86_0/libs/filesystem/doc/index.htm). This example is adapted from [tutorial 4](https://www.boost.org/doc/libs/1_86_0/libs/filesystem/example/tut4.cpp) of Boost Filesystem.
2629
- `fem` defines a class to solve the Poisson problem with the finite-element method (FEM) using [deal.II](https://www.dealii.org/). Output is written to a file `solution.vtk`, which can be visualized with, for example, [Paraview](https://www.paraview.org/). This example is adapted from the [tutorial step 3](https://dealii.org/current/doxygen/deal.II/step_3.html) of deal.II.
2730
- `yamlParser` adds a function to parse a simple yaml file using [yaml-cpp](https://github.com/jbeder/yaml-cpp) and to output the value of the key `version`. The folder also contains an example file `config.yml`.
2831

29-
Further resources:
30-
31-
- `inittimezone`: A bash script that comes handy when inheriting from Ubuntu Docker images. More information on what to do with it below.
32-
33-
## CMake
34-
35-
Please choose a meaningful project name. As target name, use `main`. We recommend to follow best practice and create a `build` directory at the root level from which `cmake ..` is called.
36-
3732
## Docker Setup
3833

39-
The code and all dependencies should run in a Docker container based on the `ubuntu:22.04` image. As by now, you already know how to set up a basic Docker container, we do no longer provide detailed instructions. We recommend to build the Dockerfile incrementally. Start with a rather empty one and install dependencies manually in the interactive mode. Take notes of the commands you use, so you can integrate them into the Dockerfile afterwards and rebuild your image.
40-
41-
To prevent the image from asking the timezone in some dialog, use ...
42-
43-
```docker
44-
COPY inittimezone /usr/local/bin/inittimezone
45-
RUN inittimezone
46-
```
47-
48-
... **before** you install new packages. Otherwise the Docker image creation will be stuck at a point that requires user interaction while it is not possible to interact with the process. You might need make `inittimezone` executable first: `chmod +x inittimezone`.
34+
The code and all dependencies should run in a Docker container based on the `ubuntu:24.04` image. As by now, you already know how to set up a basic Docker container, we do no longer provide detailed instructions. We recommend building the Dockerfile incrementally. Start with a rather empty one and install dependencies manually in the interactive mode. Take notes of the commands you use, so you can integrate them into the Dockerfile afterwards and rebuild your image.
4935

5036
Some standard packages available on Aptitude might come handy:
5137

5238
- `build-essential`
5339
- `cmake`
54-
- `git`
40+
- `unzip`
5541
- `wget`
5642
- `vim`
5743

@@ -60,5 +46,5 @@ Some standard packages available on Aptitude might come handy:
6046
Add dependencies one by one: Comment in the parts of `main.cpp` that are connected to a specific dependency. Then, install the dependency and extend the `CMakeLists.txt`. Verify that you can build and run the executable. If everything works, go on and include the next dependency.
6147

6248
- Maybe start with the boost dependencies. Boost Container is a header-only dependency, Boost Filesystem needs to be linked. Both are available in `libboost-all-dev`. There is a CMake module to [find boost libraries](https://cmake.org/cmake/help/latest/module/FindBoost.html).
63-
- deal.II is available in `libdeal.ii-dev`. deal.II uses some fancy [CMake macros](https://www.dealii.org/current/users/cmake_user.html).
64-
- yaml-cpp is an optional bonus task. For some arbitrary reason, we are not happy with the latest release of the software (which would be available through Aptitude), but we want to use the current `master` branch [directly from GitHub](https://github.com/jbeder/yaml-cpp). Get it via `git clone`, and build and install (`make install`) it yourself. Do not forget to add the necessary instructions to the Dockerfile. Sometimes containers behave weirdly: If libraries in `/usr/local/lib` are not found by CMake, please add the path to the environment variable `LD_LIBRARY_PATH`.
49+
- deal.II is available in `libdeal.ii-dev`. deal.II uses some specific [CMake macros](https://www.dealii.org/current/users/cmake_user.html).
50+
- yaml-cpp is an optional bonus task. For some arbitrary reason, we are not happy with the latest release of the software (which would be available through Aptitude), but we want to use exactly version `v0.6.3` [directly from GitHub](https://github.com/jbeder/yaml-cpp/releases/tag/yaml-cpp-0.6.3). Get the sources with [wget](https://linuxize.com/post/wget-command-examples/) and build and install (`make install`) it yourself. Do not forget to add the necessary instructions to the Dockerfile. If libraries in `/usr/local/lib` are not found by CMake, please add the path to the environment variable `LD_LIBRARY_PATH`.

03_building_and_packaging/cmake_slides.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ slideOptions:
3333

3434
## Learning Goals of This Unit
3535

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

@@ -54,7 +54,7 @@ slideOptions:
5454

5555
## Are There Alternatives?
5656

57-
> Yes, `autotools`, `scons`, ...
57+
> Yes, `autotools`, `scons`, `meson`, `bazel`, ...
5858
5959
---
6060

@@ -67,9 +67,9 @@ slideOptions:
6767
## At Least Some Reasons Why CMake is Great?
6868

6969
- CMake can generate files for many build systems: Make, ninja, VSCode project, Eclipse project, ...
70-
- Many **GUIs** and **TUIs**: `ccmake`, `cmake-gui`, integration in probably nearly all IDEs, ...
70+
- Many **GUIs** and **TUIs**: `ccmake`, `cmake-gui`, integration with IDEs, ...
7171
- CMake is **cross-platform**: you can ideally use same CMake file in all OS (easy to distinguish platform-specific things).
72-
- CMake build directory independent of project source directory (build multiple versions with different dependencies, different build types, etc.)
72+
- CMake build directory independent of project source directory (build multiple versions with different dependencies, build types, ...)
7373
- CMake respects choices in user environment (e.g. user defines cpp compiler through `CXX`).
7474
- Wide language support
7575

@@ -168,3 +168,4 @@ slideOptions:
168168
- [An Introduction to Modern CMake](https://cliutils.gitlab.io/modern-cmake/)
169169
- [Professional CMake](https://crascit.com/professional-cmake/), an up-to-date (non-public) book
170170
- [CMake Cookbook](https://github.com/dev-cafe/cmake-cookbook)
171+
- [Code Refinery CMake course](https://coderefinery.github.io/cmake-workshop/)
Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,4 @@
1-
From ubuntu:22.04
2-
3-
COPY inittimezone /usr/local/bin/inittimezone
4-
5-
# Run inittimezone and install a few dependencies
6-
RUN apt-get -qq update && \
7-
inittimezone && \
8-
apt-get -qq -y install \
9-
build-essential \
10-
g++ \
11-
vim
1+
FROM ubuntu:24.04
122

133
ADD print_my_environment_variable.sh .
144
CMD ["/bin/bash"]

03_building_and_packaging/examples/environment-variables/inittimezone

Lines changed: 0 additions & 16 deletions
This file was deleted.

03_building_and_packaging/linux_fundamentals_demo.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Example code is in [`building-and-packaging/material/examples/shared-and-static-
1515
- Run `ldd` on `main-shared` and `main-static`.
1616
- There will several libraries linked to `main-shared`.
1717
- When using `ldd` on `main-static`, we get the message `not a dynamic executable`.
18+
- Easier to grasp: `libtree -v main-shared`, [libtree](https://github.com/haampie/libtree)
1819
- Run `ls -lah` to show different executable sizes:
1920

2021
```bash
@@ -30,14 +31,11 @@ Example code is in [`building-and-packaging/material/examples/shared-and-static-
3031

3132
- `/`: primary root
3233
- `/home`: contains user's home directories
33-
- `/bin`: executables important for the system/OS
34-
- `/sbin`: executables important for the system/OS to be executed by the super user
35-
- `/usr/`: second hierarchy level containing things not required by the OS:
36-
- `/usr/lib/`
37-
- `/usr/bin/`, `/usr/sbin/`
38-
- `/usr/include/`
39-
- `/usr/local`: third level hierarchy level containing packages built manually
40-
- ...and more directories. Details and meaning can be found on [homepage of FHS](https://refspecs.linuxfoundation.org/fhs.shtml).
34+
- `/bin`: executables installed by package manager
35+
- `/sbin`: executables installed by package manager, executed by the super user
36+
- `/lib`: libraries installed by package manager
37+
- `/usr/`: historic reasons, often today simply symbolic links
38+
- `/usr/local`: third level hierarchy level containing packages installed manually
4139

4240
## Environment Variables
4341

@@ -49,6 +47,7 @@ Example code is in [`building-and-packaging/material/examples/environment-variab
4947
- `ls`
5048
- `ls usr`
5149
- `ls usr/local`
50+
- `echo $PATH`
5251
- Show all environment variables: `env`
5352
- Show script `print_my_environment_variable.sh`
5453
- Use script:

03_building_and_packaging/linux_fundamentals_slides.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,9 @@ SlideOptions:
7373

7474
## Filesystem Hierarchy Standard
7575

76-
- Defines filesystem layout and location of common files on Linux
77-
<img src="https://raw.githubusercontent.com/Simulation-Software-Engineering/Lecture-Material/main/03_building_and_packaging/figs/filesystem_hierarchy_structure/fig.png" width=100%; style="margin-left:auto; margin-right:auto; padding-top: 25px; padding-bottom: 25px">
76+
- Defines filesystem layout on Linux
77+
<img src="https://raw.githubusercontent.com/Simulation-Software-Engineering/Lecture-Material/main/03_building_and_packaging/figs/filesystem_hierarchy_structure/fig.png" width=80%; style="margin-left:auto; margin-right:auto; padding-top: 25px; padding-bottom: 25px">
78+
- Often symbolic links between 1st and 2nd hierarchy: `bin -> usr/bin`
7879
- [Official homepage of FHS](https://refspecs.linuxfoundation.org/fhs.shtml)
7980

8081
---

03_building_and_packaging/make_demo.md

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,6 @@ Example code is in [`building-and-packaging/material/examples/make`](https://git
66

77
Show `main.cpp` and build by hand `g++ -o helloworld main.cpp`
88

9-
```cpp
10-
#include <iostream>
11-
12-
int main()
13-
{
14-
std::cout << "Hello World!" << std::endl;
15-
return 0;
16-
}
17-
```
18-
199
## Single Rule Example
2010

2111
- Remove `helloworld`.
@@ -64,13 +54,12 @@ helloworld : main.cpp sse.o
6454
- Run `make`, only builds `sse.o`
6555
- By default, first target is built.
6656
- `make helloworld` to build specific target
67-
- phony target (a helper target, doesn't correspond to a file)
6857

6958
```diff
7059
+ all : helloworld sse.o
7160
```
7261

73-
- Why does Make not just build directly build all targets? We could want to do different things with different targets.
62+
- Why does Make not just directly build all targets? We could want to do different things with different targets.
7463
- `all` typically comes first.
7564
- Add `clean` target, has no dependency.
7665

@@ -81,6 +70,7 @@ helloworld : main.cpp sse.o
8170

8271
- Run `make clean`
8372
- `mkdir clean` and `make clean` confuses Make.
73+
- phony target (a helper target, doesn't correspond to a file)
8474

8575
```
8676
+ .PHONY : all clean

03_building_and_packaging/make_slides.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ slideOptions:
3333

3434
## Learning Goals of This Unit
3535

36-
- Understand the basic functionality of makefiles (timestamps, dependencies, update rules).
36+
- Explain the basic functionality of makefiles (timestamps, dependencies, update rules).
3737
- Read simple makefiles and know where to look for additional material for complex makefiles.
3838
- Write simple makefiles for small projects.
3939

@@ -50,7 +50,7 @@ Introduce `Hello-World` example
5050
- A build system
5151
- The / a go-to solution for small (research) projects (e.g., latex document, processing data, ...), though also used in big projects ([Linux kernel](https://github.com/torvalds/linux))
5252
- A building block for CMake
53-
- Nice non-expert introduction in [py-RSE book, chapter 9](https://merely-useful.tech/py-rse/automate.html)
53+
- Nice non-expert introduction in [py-RSE book, chapter 9](https://third-bit.com/py-rse/automate.html)
5454
- [GNU Make](https://www.gnu.org/software/make/): standard implementation of Make for Linux and macOS
5555

5656
---
@@ -82,7 +82,7 @@ Introduce `Hello-World` example
8282
- Wildcards
8383
- ... but becomes quickly [very hard to read](https://www.gnu.org/software/make/manual/html_node/Automatic-Variables.html).
8484
- Not covered because CMake does this for us.
85-
- But nicely documented in [py-RSE chapter 9](https://merely-useful.tech/py-rse/automate.html).
85+
- But nicely documented in [py-RSE chapter 9](https://third-bit.com/py-rse/automate.html).
8686

8787
---
8888

0 commit comments

Comments
 (0)