Reputation: 1
Following are three files,
/* HANDLERS.H */
#define TRUE 1
#define FALSE 0
/* Functions declarations - included in both UI.C & tiffhandler.c */
int canHandle(char *);
int drawImage(char *);
int saveFile(char *);
/* tiffhandler.c */
#include "HANDLERS.H"
int canHandle(char *fileName){
return TRUE;
}
int drawImage(char *fileName){
return TRUE;
}
int saveFile(char *fileName){
return TRUE;
}
/* UI.C */
#include "HANDLERS.H"
int main(void){
}
that are compiled as,
>gcc HANDLERS.H tiffhandler.c UI.C
My question,
HANDLERS.H
is included in both UI.C
& tiffhandler.c
. So, function declarations are included in both.
During linking phase, before tiffhandler.o
& UI.o
are linked, Why linker does not complain, by saying, multiple function declarations for each function(say canHandle
)?
Upvotes: 0
Views: 69
Reputation: 33924
The linker will only fail if you have multiple function definitions in a compilation unit. So even the following is ok in a file:
void func(void);
void func(void);
void func(void);
Upvotes: 1
Reputation: 168726
Because multiple function declarations are perfectly legal. Only multiple function definitions are disallowed.
It is okay to repeat the declaration:
int canHandle(char *);
int canHandle(char *);
But the following is incorrect since it repeats a definition:
int canHandle(char *fileName){
return TRUE;
}
int canHandle(char *fileName){
return TRUE;
}
The prohibition against multiple definitions of the same function is enforced by both the compiler (inside a single translation unit) and the linker (across multiple translation units).
Upvotes: 5