Header Files¶
Declarations: Problems (1)¶
Declarations must exactly match corresponding definitions
extern int g_lobal;
void print_g(void);
void main(void)
{
g_lobal = 100;
print_g();
}
|
double g_lobal;
void print_g(const char[] format})
{
...
}
|
Declarations: Problems (2)¶
Severe bugs
Incorrect linkage: perception of user does not match definition
Hard to detect: no tool support — only discipline and conventions
At best: segmentation fault ⟶ crash
At worst: appears to work, but in fact doesn’t
Solution
Centralize declarations ⟶ header files
#include "g.h"
, rather than giving declarations by hand
Declarations: Solutions¶
#ifndef G_H
#define G_H
extern double g_lobal;
void print_g(
const char[] format);
#endif
|
// have compiler check
// declaration/definition
// consistency
#include <g.h>
double g_lobal;
void print_g(
const char[] format)
{
...
}
|
main.c
#include "g.h"
...