Theory
Common mistakes
With modules most mistakes aren't about logic but about what is written in which file.
Common mistakes
- A function body in a
.hfile. While the.his included in one.c, everything works; with a second one you getmultiple definition. A.hholds declarations only. #include "daytime.c"..cfiles aren't included: they're listed inCMakeLists.txt. Otherwise the functions are defined twice:multiple definition.- A
.cfile missing from the target.undefined reference to ...– check theadd_executable/add_librarylist. CMake doesn't notice a new file by itself. - A forgotten
#include.implicit declaration of function– the compiler doesn't see the declaration. Include the module's.h. - The module's
.cdoesn't include its own.h. Then the declaration and the definition can drift apart, and nobody notices until the program starts behaving oddly. The first line ofdaytime.cis#include "daytime.h". - No
#ifndefguard. Today maybe nothing happens, but in topic 10, when a.hdescribes a structure, a second inclusion will be an error. Every.hgets a guard, named after the file:DAYTIME_H. - A variable in a
.hfile.int counter;in a header gives the samemultiple definition. Data travels as parameters. - A helper function without
static. It is visible to the whole program, and another module with the same name causesmultiple definition. Whatever isn't in the.hfile isstatic.