As the title says, I know what causes this error but I want to know why the compiler gives it in this circumstance.
Eg :
main.c
void test(){
test1();
}
void test1(){
...
}
Would give an implicit declaration warning as the compiler would reach the call to test1() before it has read its declaration, I can see the obvious problems with this (not knowing return type etc), but why can’t the compiler do a simple pass to get all function declarations, then compile the code removing these errors? It just seems so simple to do and I don’t believe I’ve seen similar warnings in other languages.
Does anyone know if there is a specific purpose for this warning in this situation that I am overlooking?
Short answer: Because C is ooooold. 🙂
Long answer: The C compiler and linker are totally separate. You might be defining different functions across different source files, and then linking them together. In this case, say that you were defining
test1in a separate library source file. The compiler wouldn’t know abouttest1until it has compiled the other file, and it compiles the other file separately so it can’t know about it when it’s compilingtest. Therefore you have to tell it, ‘yes, there really is atest1defined elsewhere, and here is its signature’. That’s why you usually include a header file (.h), for any other source files whose functions you need to use, in this one.