1.9. Use common header file¶
To fix the compiler warning as shown in Compiler Flags — Warning we curdely modified main.c
. But that is not the correct way to fix the problem for missing declaration of greeting()
.
incorrect main.c¶
extern void greeting(void);
int main() {
greeting();
return 0;
}
The correct way would be to use a common header file between the consumer, main.c
and the provider greeting.c
main.c with header inclusion¶
#include "greeting.h"
int main() {
greeting();
return 0;
}
greeting.h¶
#ifndef GREETING_H
#define GREETING_H
void greeting(void);
#endif /* GREETING_H */
greeting.c (With header include)¶
#include <stdio.h>
#include "greeting.h"
void greeting() {
printf ("Hello World!\n");
}
To compile that file with GCC, the command would be the same as used earlier:
gcc main.c greeting.c -Wall -Werror -o greeting
As usual, we should not see any compiler warnings during this compilation step.