0
0
Cprogramming~10 mins

Linking multiple files in C - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Linking multiple files
Write code in file1.c
Write code in file2.c
Compile file1.c and file2.c separately
Link object files together
Create executable
Run executable
Code is written in separate files, compiled into object files, then linked together to form one executable program.
Execution Sample
C
#include <stdio.h>

// file1.c
void greet();

int main() {
    greet();
    return 0;
}

// file2.c
#include <stdio.h>
void greet() {
    printf("Hello from greet!\n");
}
Two files: one with main calling greet(), the other defining greet(). They are compiled and linked to run together.
Execution Table
StepActionFileResult/Output
1Write main() calling greet()file1.cmain() calls greet()
2Write greet() functionfile2.cgreet() prints message
3Compile file1.cfile1.cCreates file1.o (object file)
4Compile file2.cfile2.cCreates file2.o (object file)
5Link file1.o and file2.olinkerCreates executable program
6Run executableexecutableOutput: Hello from greet!
7Program endsexecutableReturn code 0
💡 Program ends after printing message and returning 0
Variable Tracker
Variable/FunctionStartAfter Step 1After Step 2After Step 6Final
main()Not definedDefined in file1.cDefined in file1.cCalled and runsEnds
greet()Not definedDeclared in file1.cDefined in file2.cCalled by main(), prints messageEnds
OutputNoneNoneNone"Hello from greet!\n"Printed
Key Moments - 3 Insights
Why does main() know about greet() even though greet() is in another file?
Because main() has a declaration of greet() (a promise) and the linker connects main() with greet() from the other file as shown in steps 3-5 in the execution_table.
What happens if we forget to link the object files together?
The linker will give an error saying it cannot find greet(), because the function is defined in another file and linking is needed to combine them (see step 5).
Why do we compile files separately before linking?
Compiling separately creates object files for each source file, which the linker then combines. This allows modular code and faster builds (steps 3 and 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step is the executable created?
AStep 5
BStep 3
CStep 6
DStep 4
💡 Hint
Check the 'Action' column for linking object files into executable (step 5).
According to variable_tracker, what is the state of greet() after step 2?
ANot declared
BDeclared in file1.c only
CDefined in file2.c
DCalled and runs
💡 Hint
Look at the 'greet()' row under 'After Step 2' in variable_tracker.
If we skip compiling file2.c, what will happen during linking?
ALinker will succeed with no errors
BLinker will fail due to missing greet() definition
CProgram will run but greet() prints nothing
DCompilation will fail
💡 Hint
Refer to key_moments about linking errors when definitions are missing.
Concept Snapshot
Linking multiple files in C:
- Write code in separate .c files
- Declare functions in one file, define in another
- Compile each file to object files (.o)
- Link object files to create executable
- Run executable to see combined program output
Full Transcript
Linking multiple files means writing code in separate C files and combining them to run as one program. First, you write main() in one file and a function like greet() in another. You compile each file separately into object files. Then, the linker combines these object files into one executable. When you run the executable, main() calls greet(), and the program prints the message. This process allows you to organize code better and reuse functions across files. If you forget to link, the program won't find functions defined elsewhere and will give errors.