Something about programming

Header files and multi-file sources in C language

Previous tutorial: Functions in C language

As C programming language is very old, it has obsolete and not intuitive capabilities in creating program that consists from multiple files with source code. In this tutorial we'll learn how to build such program and learn about the role of header files in C language.

Header files in C language

When we run compiler, it compiles the source code files separately (if we have more than one) into object files. Object files contains binary code. After that, another program, that is called linker, links object files together into executable file.

In our first program we saw such string:

#include <stdio.h>

#include is one of the examples of preprocessor directives. Preprocessor directives are executed before compiler starts it's work. #include inserts contents of the specified file at the place where include directive is. So, stdio.h is a file. C language requires including of header files.

Header files contains "description" of functionality. The actual implementation is located in another binary file. I.e. to use functionality of external source code file we need to include header file that describes what this file can do. stdio.h contains function declarations that are related to input/output.

Include directive can behave different depending of syntax:

#include <stdio.h> #include "stdio.h"

When we enclose file in <> preprocessor will try to find the file in directories where compiler stores it's standard libraries. When we use "", preprocessor will check first directory with the source code of your program and when will try to search in standard library directories. You need to use "" when you include your own header files.

Header files mostly contains declarations of functions. So, when you use external function in your own code, compiler sees that there is the declaration and what arguments function can receive. Later linker will link the binary code that contains definition of function to your own code.

Multi-file program in C language

Let's try it. We'll have file with the main function, the file that will have another function and header file for this function.

// multi-file.c #include "hello.h" int main(void) { hello("Roman"); return 0; } // hello.h void hello(char*); // hello.c #include <stdio.h> void hello(char* name) { printf("Hello, %s!\n", name); }

We have separate file hello.c that contains definition of function hello that prints text. Pay attention that we included in this file stdio.h as we use function printf. File hello.h contains declaration of function. In the file multi-file.c we include hello.h and later we use function hello.

Compiling multiple source files

Now let's compile our program with gcc:

gcc -o multi-file multi-file.c hello.c

We use parameter -o to set the output executable file and after that we list all source code files. We don't need to list header files, preprocessor will find them automatically.


Our programs will become larger and larger, so we'll need to divide code into separate files and now we know how to do it.


No comments yet