1.6. Building with two files

Step by step let’s make things a little more advanced. Rather than compiling just one file, let us compile two source files together. So, let us compile two source files, main.c and greeting.c

int main() {
    return 0;
#include <stdio.h>

void greeting() {
    printf ("Hello World!\n");

In this case, the actual logic is with greeting.c (printing the greeting). And, main.c is consuming / using / triggering that logic.

To compile both the files with GCC to create a single executable, the command would be:

gcc main.c greeting.c -o greeting

With the above command, we are telling GCC that we want to compile main.c and greeting.c and the output should be greeting


When the above command completes successfully, but we still get a compiler warning. We will handle that in a bit later.

main.c: In function ‘main’:
main.c:2:5: warning: implicit declaration of function ‘greeting’ [-Wimplicit-function-declaration]
    2 |     greeting();
      |     ^~~~~~~~

As we did earlier in Basic Compilation, to execute the singe binary generated from the two source file, the command would be:


If everything went fine, the output would be:

Hello World!

As you can see, the compiler has given us a warning. Even though, we are able to compile the source code. Although the final output is same as Basic Compilation, as programmers we should always remember that a warning is a warning. It must be fixed.

To compile two files on windows with Microsoft Visual C Compiler cl.exe, the command would be

cl.exe main.c greeting.c /nologo /Fegreeting.exe

When the above command completes successfully, the console output would be.

Generating Code...

Since we are compiling two files, we see names of two files being compiled.

As we did in previous section, to run the generated binary, we can use:

call greeting.exe

The output would be same as that we would have got when compiled with gcc:

Hello World!

If you compare gcc and cl, one compiler is giving warning, while the other is not. That is because by default cl uses warning level as /W1. In the next section we will introduce the compiler option /W4