C header files and compilation/linking

Uchia Itachi gave the answer. It’s the linker.

Using GNU C compiler gcc you would compile a one-file program like

gcc hello.c -o hello # generating the executable hello

But compiling the two (or more) file program as described in your example, you would have to do the following:

gcc -c func.c # generates the object file func.o
gcc -c main.c # generates the object file main.o
gcc func.o main.o -o main # generates the executable main

Each object file has external symbols (you may think of it as public members). Functions are by default external while (global) variables are by default internal. You could change this behavior by defining

static int func(int i) { # static linkage
    return ++i ;


/* global variable accessible from other modules (object files) */
extern int global_variable = 10; 

When encountering a call to a function, not defined in the main module, the linker searches all the object files (and libraries) provided as input for the module where the called function is defined. By default you probably have some libraries linked to your program, that’s how you can use printf, it’s already compiled into a library.

If you are really interested, try some assembly programming. These names are the equivalent of labels in assembly code.

Leave a Comment