From text to a program
How text becomes a program
A processor runs only machine code. Your main.c is text, so it has to be translated before it can run. That is the job of the compiler: in our case GCC from MinGW, which is already inside CLion.
- The preprocessor pastes in the
#includefiles:#include <stdio.h>brings in the declaration ofprintf. - The compiler checks that the code follows the rules of C and translates it into machine code. Almost every error you will see comes from it.
- The linker joins your code with libraries (the code of
printf, for example, is already written for you) into one executable file.
CLion does all of this with one button, ▶: it builds and runs. How to build the program is described by the CMake file CMakeLists.txt.
Every warning is an error
The course's CMakeLists.txt gives the compiler these flags:
-std=c17 -Wall -Wextra -Werror -pedantic -Wvla
-std=c17: pure standard C17, without GNU extensions;-Wall -Wextra -pedantic: turn on almost every warning;-Wvla: forbids variable-length arrays (int t[n];, topic 6);-Werror: every warning becomes an error, and the program does not build until you fix it.
At first this is annoying. But a warning is almost always a bug that hasn't shown up yet, and this way you catch it at once.
Your repository
You do all your work in one GitHub repository, created by GitHub Classroom:
- Open the Classroom link from your lecturer and click Accept this assignment. GitHub creates a repository for you from the course template.
- On CLion's welcome screen click Clone Repository (or File → New → Project from Version Control), paste the repository address and click Clone.
- CLion opens the project and asks which CMake profiles to enable: keep Debug (CLion, MinGW). In the CMake window (at the bottom) you should see
Build files have been written to: .../build/debug.
The repository already has:
CMakeLists.txt the course build (no need to change it)
CMakePresets.json Debug, Release and CI settings
include/input.h safe input (from topic 2)
src/input.c
practice/ your lesson programs go here
hw/01/ topic 1 homework with tests
tests/ tests of input.h
.github/workflows/ tests on GitHub after every push
Every task is one commit
Next to the code tasks you will see the course checklist. Its first item: every task is one commit. Finished a task → Ctrl+K, write a short message, e.g. Add first program, and click Commit and Push.
After every push GitHub builds your code on Linux with AddressSanitizer and runs the tests. You see the result in your repository's Actions tab: what red and green mean there, we go through in the homework.