Multi-File Programs
#include <stdio.h> /* Required for printf */
int main(void)
{
printf("Hello, world!\n");
return 0;
}
/* Function prototype */
int printf(const char *, ...);
int main(void)
{
printf("Hello, world!\n");
return 0;
}
Multi-File Example
Recall this function to find the average of two numbers:And we want to use it in our program. This is main.c:float average(float a, float b) { return (a + b) / 2; }
#include <stdio.h> /* printf */
int main(void)
{
float ave = average(4.0F, 5.0F);
printf("The average is %.2f\n", ave);
return 0;
}
Of course, this is going to cause problems:
We have a warning from the compiler and an error from the linker:gcc -Wall -Wextra -ansi -pedantic main.c
We can fix the compiler warning by simply providing a prototype (declaration) for average:main.c: In function 'main': main.c:8:15: warning: implicit declaration of function 'average' [-Wimplicit-function-declaration] float ave = average(4.0F, 5.0F); ^~~~~~~ C:\Users\mmead\AppData\Local\Temp\ccyQlmin.o:main.c:(.text+0x26): undefined reference to `average' collect2.exe: error: ld returned 1 exit status
#include <stdio.h> /* printf */
/* Function prototype */
float average(float x, float y);
int main(void)
{
float ave = average(4.0F, 5.0F);
printf("The average is %.2f\n", ave);
return 0;
}
Of course, this still causes the linker to complain:
We need to tell the linker where to find the function average. First, we need to compile the code that contains the function average. Assume this is in average.cC:\Users\mmead\AppData\Local\Temp\ccKLV9Mi.o:main.c:(.text+0x1e): undefined reference to `average' collect2.exe: error: ld returned 1 exit status
Compiling it: (Note the -c switch which will compile the file, but not link. THIS IS IMPORTANT.)float average(float a, float b) { return (a + b) / 2; }
Had we not used the -c switch, we would have encountered this fatal error:gcc -Wall -Wextra -ansi -pedantic -c average.c
The linker is telling us that it can't find a function called main. This makes sense, since average.c doesn't contain a main function and all programs need a main function.C:/mingw/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/7.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x2e): undefined reference to `WinMain' collect2.exe: error: ld returned 1 exit status
We just need to make sure that we perform all of the steps:
And we can do this in one step: (Compile both C files and link them together to form an executable program.)gcc -Wall -Wextra -ansi -pedantic -c main.c -o main.o <--- compile main.c to main.o gcc -Wall -Wextra -ansi -pedantic -c average.c -o average.o <--- compile average.c to average.o gcc main.o average.o -o program <--- link main.o and average.o to program (program.exe on Windows)
We can see see the files on the disk and their relative sizes:gcc -Wall -Wextra -ansi -pedantic main.c average.c -o program
9/17/2019 08:35a 62 average.c 9/17/2019 08:56a 209 main.c 9/17/2019 09:15a 458 average.o 9/17/2019 09:15a 650 main.o 9/17/2019 09:14a 9,089 program.exe