I am trying to understand why we need to include prototypes of functions in the source code. From my understanding: In order to obtain an executable file from multiple source files, the source files need to be transfromed to object files. Object files can refer to each other without any problem: for example, a the main file can call the foo function which will be compiled from another source file. The linker is in charge to resolve all the references to the various functions / symbols from all the source files.
I sucessfully compiled this two files independantly and then produced the executable. You can notice there is no #include "function.h" in the main.c
function.c
int foo() {
return 1;
}
main.c
int main() {
return foo();
}
commands used:
gcc mainc.c function.c -S
gcc main.o function.o -o exec
I get a warning with the first command, due to implicit declaration of the function foo, but the output exec is working. So my question is:
Why do we need to include prototypes of functions?