Suppose we have two files.
functions.c:
#include <stdio.h>
void greet(void) {
printf("Hi!");
}
main.c:
int main() {
// Empty
}
And we need to call greet() from main.c. How do we do that? π€
If you are coming from other languages, you might expect to import functions.c in main.c and call greet().
But C programmers usually create a file named functions.h first, containing the top portion of the function (called declaration):
functions.h:
void greet(void);
Then #include it in main.c and call the function:
main.c:
#include "functions.h"
int main() {
greet();
}
Why not #include functions.c directly? Why use a header file (functions.h)?
How #include works
The working mechanism of #include "filename" is extremely simple--It just copies that file's contents into that spot before actual compilation begins (literally). main.c would look like this just before compilation:
main.c:
void greet(void); // <-- Contents of functions.h
int main() {
greet();
}
So functions.h is not strictly needed here, as long as you put void greet(void); (the declaration for the function greet()) in main.c. Like this:
main.c:
void greet(void); // <-- Declaration for greet()
int main() {
greet();
}
So the question becomes, why we need to declare functions.
Why do we need to declare functions? π€
When we compile our program, we already specify all source files.
Example build command (using the C compiler gcc):
gcc main.c functions.c -o final_program
Since we already specify all source files here, manual import inside the files (like Python) isn't needed. We do not import source files inside source files, we simply build (compile) them together.
The key part
But each one of those source files compile separately, often in parallel so as to utilise the multiple cores in your CPU. In our case, main.c and functions.c get compiled in parallel. This is the main reason why we need to declare functions.
Lets assume the compiler is compiling this part of main.c:
greet();
Here, we are calling a function which is not defined in the current file. The compiler doesn't know how to call greet(), like which arguments to pass, the types of the arguments, the return type, etc. Without that info, the compiler wouldn't be able to translate it to machine code
When we add a declaration void greet(void); in main.c, we are promising the compiler that a function named greet that takes no arguments and return nothing exists in one of the source files (note: void means "nothing"). Now, the compiler has our promise so it doens't throw an error saying that this function doesn't exist, as well as the necessary information to compile the calling part. The address to the actual function is left empty.
After compilation, each source file becomes an object file, with the file extension .o (main.o, functions.o).
Then, something called a linker, combines these object files together, giving us the final executable. It will fill in the empty function addresses, and throws an error if the function we promised to exit doesn't exist in any of the source files.
So, that's solved now. But we didn't answer the original question, did we?
Why Can't We Just #include "functions.c in main.c?
We said:
"Each one of those source files compile separately, often in parallel so as to utilise the multiple cores in your CPU."
But if we #include the contents of all files in main.c, main.c would be a single large file before compilation, and we would loose that benefit.
Also, compilers reuse object files. Assume we compiled our project first, then made a few changes to functions.c, and recompiled. The compiler wouldn't compile main.c again to main.o! It detects that main.c wasn't changed, and reuses main.o. If we had #includeed functions.c in main.c, this wouldn't have been possible, since the compiler sees a single, concatenated main.c file.
There is also another problem that happens if you #include "functions.c" in more than one file. Because #include just copies the code in, greet() gets defined more than once across your compiled files, and the linker throws a multiple definition error when combining them.
Putting #pragma once in functions.c wouldn't fix this, because it only stops a file from being included more than once inside the same single file during one compilation. It does not stop if separate .c files #include functions.c and create duplicate definitions for the linker.
Thanks for reading π
This is my first post for Necoz and was inspired by @csm18!
I wrote this entire article from my phone during a bus commute. Feel free to report if there are any typos/issues! π
Top comments (2)
Really enjoyed this explanation! It's refreshing to see someone explain why C works this way rather than simply telling beginners to use header files and move on.
The distinction between compilation and linking is one of those fundamentals that becomes much more valuable as projects grow.
It also makes me wonder what you think about unity builds, where developers intentionally combine multiple source files to improve compilation performance. At what point do the benefits outweigh the drawbacks of separate compilation?
And writing this entire article on your phone during a bus commute? That's impressive. I can barely write a decent commit message on mine. π
Looking forward to more articles like this!
I'm quite noob in mobile too.
I was waiting for this!