Bird
Raised Fist0
Cprogramming~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of linking multiple files in a C program?
easy
A. To organize code into separate files and combine them into one program
B. To run multiple programs at the same time
C. To convert C code into machine language
D. To write comments in different files

Solution

  1. Step 1: Understand code organization

    Linking multiple files allows splitting code into parts for better organization and reuse.
  2. Step 2: Understand linking process

    After compiling separately, linking combines these parts into one executable program.
  3. Final Answer:

    To organize code into separate files and combine them into one program -> Option A
  4. Quick Check:

    Linking = Combining files [OK]
Hint: Linking combines separate code files into one program [OK]
Common Mistakes:
  • Confusing linking with running multiple programs
  • Thinking linking converts code to machine language
  • Believing linking is for writing comments
2. Which command correctly compiles two C files main.c and utils.c together using GCC?
easy
A. gcc main.c -o program
B. gcc -o program -c main.c utils.c
C. gcc main.c utils.c -o program
D. gcc program -o main.c utils.c

Solution

  1. Step 1: Understand GCC command structure

    The correct syntax is: gcc [source files] -o [output file].
  2. Step 2: Check options order

    The standard GCC syntax is gcc [source files] -o [output], as in gcc main.c utils.c -o program.
  3. Final Answer:

    gcc main.c utils.c -o program -> Option C
  4. Quick Check:

    gcc source files -o output [OK]
Hint: Put source files first, then -o output [OK]
Common Mistakes:
  • Placing -o before source files
  • Mixing order of files and output option
  • Using output file name as source file
3. Given two files:
// file1.c
#include 
void greet() { printf("Hello from file1\n"); }

// file2.c
void greet();
int main() { greet(); return 0; }

What is the output after compiling and linking both files?
medium
A. Compilation error: greet not declared
B. Hello from file1
C. Runtime error: undefined reference to greet
D. No output

Solution

  1. Step 1: Check function declaration and definition

    file1.c defines greet() printing "Hello from file1"; file2.c declares greet() and calls it in main.
  2. Step 2: Linking files

    Compiling and linking both files connects the call to greet() with its definition, so it prints the message.
  3. Final Answer:

    Hello from file1 -> Option B
  4. Quick Check:

    Function call linked correctly = prints message [OK]
Hint: Declare functions in headers or files to link correctly [OK]
Common Mistakes:
  • Forgetting to link both files
  • Missing function declaration causing errors
  • Expecting runtime errors without linking
4. What is wrong with this setup?
// file1.c
void greet() { printf("Hi\n"); }

// file2.c
int main() { greet(); return 0; }

When compiling separately and linking, it gives an error. Why?
medium
A. Cannot compile multiple files separately
B. Missing #include in file1.c
C. main() should be in file1.c
D. greet() is not declared before use in file2.c

Solution

  1. Step 1: Check function declaration in file2.c

    file2.c calls greet() but does not declare it or include a header with its prototype.
  2. Step 2: Understand compiler error

    Without declaration, compiler errors occur because it doesn't know greet() signature.
  3. Final Answer:

    greet() is not declared before use in file2.c -> Option D
  4. Quick Check:

    Declare functions before use [OK]
Hint: Always declare functions before calling them [OK]
Common Mistakes:
  • Assuming definition is enough without declaration
  • Ignoring missing #include (not cause here)
  • Thinking main() must be in same file as greet()
5. You have three files:
// math_ops.c
int add(int a, int b) { return a + b; }

// math_ops.h
int add(int a, int b);

// main.c
#include 
#include "math_ops.h"
int main() {
  printf("Sum: %d\n", add(3, 4));
  return 0;
}

Which is the correct way to compile and link these files to produce an executable named calc?
hard
A. gcc main.c -c; gcc math_ops.c -c; gcc main.o math_ops.o -o calc
B. gcc main.c -o calc; gcc math_ops.c -o calc
C. gcc main.c math_ops.h -o calc
D. gcc main.c math_ops.c -c

Solution

  1. Step 1: Compile source files separately

    Use gcc -c to compile main.c and math_ops.c into object files main.o and math_ops.o.
  2. Step 2: Link object files

    Link main.o and math_ops.o together with gcc to create executable calc.
  3. Final Answer:

    gcc main.c -c; gcc math_ops.c -c; gcc main.o math_ops.o -o calc -> Option A
  4. Quick Check:

    Compile separately, then link object files [OK]
Hint: Compile with -c, then link object files [OK]
Common Mistakes:
  • Compiling header files directly
  • Overwriting executable by compiling twice
  • Not linking object files after compiling
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard