Example 1 — Two source files, one program
Compile both and link: gcc -Wall main.c util.c -o app.
/* util.c */
int add(int a, int b) { return a + b; }
/* main.c */
#include <stdio.h>
int add(int a, int b); /* declaration; defined in util.c */
int main(void) {
printf("add(3, 4) = %d\n", add(3, 4));
return 0;
} How It Works
main.ccompiles because the declaration tells the compiler the signature.- The call is left unresolved in
main.o. - The linker finds
addinutil.oand completes the program.
