Several files: headers and step-by-step compilation
A real program does not fit in a single file. When it grows, it is split into modules: each module has a .c file with the definitions (the code) and a .h file, the header, with the declarations (the list of what it offers). Whoever wants to use the module writes #include "name.h" and calls its functions. This separation makes the code easier to read, reuse and compile, and it is exactly the structure you will use in the final project with your contact manager.
Declaration and definition
A declaration tells the compiler that something exists and what shape it has: int clamp(int value, int low, int high); is enough for it to check the calls. A definition is the real body of the function, and there can be only one in the whole program. Headers contain declarations (prototypes, typedefs, structs, constants and macros), not function or global variable definitions. Each header carries a protection against multiple inclusion (include guard): the #ifndef, #define and #endif lines prevent the same content from being processed twice if several files include the header, which would cause redefinition errors.
/* ---- mathutil.h ---- */
#ifndef MATHUTIL_H
#define MATHUTIL_H
/* Returns value limited to the range [low, high]. */
int clamp(int value, int low, int high);
/* Returns n! for 0 <= n <= 20, or -1 if n is out of range. */
long long factorial(int n);
#endif /* MATHUTIL_H */
/* ---- mathutil.c ---- */
#include "mathutil.h"
/* static: private to this file, invisible to the linker and other modules */
static int factorial_input_ok(int n) {
return n >= 0 && n <= 20; /* 21! overflows a 64-bit integer */
}
int clamp(int value, int low, int high) {
if (value < low) {
return low;
}
if (value > high) {
return high;
}
return value;
}
long long factorial(int n) {
if (!factorial_input_ok(n)) {
return -1;
}
long long result = 1;
for (int i = 2; i <= n; i++) {
result *= i;
}
return result;
}
/* ---- main.c ---- */
#include <stdio.h>
#include "mathutil.h"
int main(void) {
printf("%d\n", clamp(15, 0, 10)); /* 10 */
printf("%d\n", clamp(-3, 0, 10)); /* 0 */
printf("%lld\n", factorial(5)); /* 120 */
printf("%lld\n", factorial(25)); /* -1 */
return 0;
}Look at the two kinds of include. With angle brackets, #include <stdio.h>, the compiler searches the system folders; with quotes, #include "mathutil.h", it looks first next to your file. And notice that mathutil.c includes its own header: that way the compiler checks that the definitions match the declarations. The word static in front of a function makes it private to the file: nobody else can call it or clash with its name. Use it for helper functions and leave without static only what you want to offer.
Compiling in steps: object and link
Compiling a multi-file program takes two phases. First each .c is compiled separately into an object file (.o) with the -c option; in this step the compiler only needs the declarations from the headers. Then the linker joins the objects and libraries and resolves each call with its real definition. This saves time: if you change only main.c, you only recompile main.o and link again, without touching the others. Always add -Wall -Wextra so the compiler warns you about problems.
# step 1: compile each source file to an object file
gcc -std=c17 -Wall -Wextra -c mathutil.c -o mathutil.o
gcc -std=c17 -Wall -Wextra -c main.c -o main.o
# step 2: link the objects into the executable
gcc mathutil.o main.o -o app
./app
# 10
# 0
# 120
# -1
# the shortcut: compile and link in one command
gcc -std=c17 -Wall -Wextra main.c mathutil.c -o appTypical errors
- undefined reference to 'factorial': you forgot to include mathutil.c or mathutil.o when linking. It is a linker error, not a compiler error.
- multiple definition of 'clamp': you put the function body in the .h and several .c files include it. Move the code to the .c and leave only the prototype.
- redefinition of 'struct X': the header's include guard is missing.
- implicit declaration of function: you call a function without including its header; add the #include.
- Shared global variable: it is declared with extern in the .h (extern int counter;) and defined once in a .c (int counter = 0;).
Test what you learned
Question 1 of 1What belongs in a header (.h)?
✎ My notes
Saved only in this browser. Sign in to keep it in your account.0/5000 characters