Show contents
Chapter 4 of 6 · 20 min
Modules, headers and make
By the end of this chapter, you'll be able to
- Separate a module's interface (.h) from its implementation (.c)
- Protect a header against multiple inclusion
- Make a function or variable private to a module with static
- Build a multi-file project with gcc and make
One module, two files
A module consists of:
- a header
.h, its interface: the declarations (prototypes, types, constants) that other files can use; - a source file
.c, its implementation: the functions' code and the internal data.
/* counter.h: what the module shows */
#ifndef COUNTER_H
#define COUNTER_H
void counter_add(int n);
int counter_value(void);
#endif
/* counter.c: how it does it */
#include "counter.h"
static int total = 0; /* private: invisible from other files */
void counter_add(int n) { total += n; }
int counter_value(void) { return total; }
/* main.c: only uses the interface */
#include <stdio.h>
#include "counter.h"
int main(void) {
counter_add(5);
counter_add(3);
printf("total = %d\n", counter_value());
return 0;
}
#include "counter.h"with quotes looks for the file in the project folder;<stdio.h>with angle brackets, in the system folders.#ifndef COUNTER_H … #endifis an include guard: if the header is included twice (directly and through another header), its content is only taken into account once.staticin front of a global variable or function makes it private to the file. Other modules can neither read nor change it: it's encapsulation, C style.- Prefixing the public functions with the module's name (
counter_…) avoids name clashes, since C has no namespaces.
Gathered into a single file, the same program runs like this:
#include <stdio.h>
/* --- counter.c: the state is private to the module --- */
static int total = 0;
void counter_add(int n) { total += n; }
int counter_value(void) { return total; }
/* --- main.c: only uses the public functions --- */
int main(void) {
counter_add(5);
counter_add(3);
printf("total = %d\n", counter_value());
return 0;
}
total = 8
Building several files
Building happens in two stages: each .c is compiled separately into an object file .o, then the linker assembles the .o files into an executable.
The executable is ready. If only main.c changes, only main.o needs recompiling.
Text description of the animation
- The preprocessor copies the header into the source file.
- gcc -c compiles this single file into machine code, without looking for the other functions.
- main.o knows counter_add exists thanks to the header, but not yet where it is.
- The linker connects each call to the matching function in the other object files.
- The executable is ready. If only main.c changes, only main.o needs recompiling.
$ gcc -std=c17 -Wall -Wextra -c counter.c # → counter.o
$ gcc -std=c17 -Wall -Wextra -c main.c # → main.o
$ gcc main.o counter.o -o counter # linking
$ ./counter
total = 8
Automating with make
A Makefile describes the files to produce and the ones they depend on. make compares modification dates and only redoes what's needed:
CC = gcc
CFLAGS = -std=c17 -Wall -Wextra -Werror
counter: main.o counter.o
$(CC) main.o counter.o -o counter
main.o: main.c counter.h
$(CC) $(CFLAGS) -c main.c
counter.o: counter.c counter.h
$(CC) $(CFLAGS) -c counter.c
clean:
rm -f *.o counter
Each rule reads "target: dependencies", followed by the command to run. Careful: the command line must start with a tab, not spaces. make builds the first target; make clean deletes the files produced. If you change counter.h, both .o files are recompiled, since they both depend on it.
Your turn
Worked exercise
Worked exercise: the stack module
Write a stack module that manages a stack of integers with a capacity of 4, with private state: stack_push(value) returns 0, or -1 if the stack is full; stack_pop(&value) returns 0, or -1 if it's empty; stack_size() returns the number of items. Write the stack.h header first, then test everything in a single file: push 10, 20, 30, 40 and 50, then pop everything.
Hint 1
static in stack.c: only the module accesses it.Hint 2
items[size++] = value stores the value then increases the size; items[--size] first decreases the size, then reads.Show the solution and common mistakes
/* stack.h */
#ifndef STACK_H
#define STACK_H
int stack_push(int value);
int stack_pop(int *value);
int stack_size(void);
#endif
#include <stdio.h>
/* --- stack.c --- */
#define STACK_MAX 4
static int items[STACK_MAX];
static int size = 0;
int stack_push(int value) {
if (size == STACK_MAX) return -1;
items[size++] = value;
return 0;
}
int stack_pop(int *value) {
if (size == 0) return -1;
*value = items[--size];
return 0;
}
int stack_size(void) { return size; }
/* --- main.c --- */
int main(void) {
for (int i = 1; i <= 5; i++) {
if (stack_push(i * 10) != 0) {
printf("stack full, %d refused\n", i * 10);
}
}
int v;
while (stack_pop(&v) == 0) {
printf("%d%s", v, stack_size() > 0 ? " " : "\n");
}
printf("final size: %d\n", stack_size());
return 0;
}
stack full, 50 refused
40 30 20 10
final size: 0
A stack gives the items back in reverse order of arrival: the last one pushed is the first one popped.
Common mistakes:
- Forgetting
staticin front ofitemsandsize: any file can then change them withextern int size;and break the stack. - Testing
size > STACK_MAXinstead ofsize == STACK_MAX: the fifth value would be written after the end of the array, an overflow GCC doesn't report.
Summary
- 1A module = a .h (the interface: declarations) + a .c (the implementation).
- 2An include guard (#ifndef … #define … #endif) avoids double inclusion in one file.
- 3static makes a global variable or function private to its file.
- 4Each .c compiles to a .o, then the linker assembles them; make only redoes what's needed.
- 5A header declares (extern), a single .c defines.
Your progress is saved on this device. Create a free account to keep your progress on all your devices.