Technical skill

Linux headers

Why C programs include headers

Before building something, a person may check a reference sheet to see which parts are available. A C program also needs a reference sheet of declarations: a header. It tells the compiler which names and types can be used, but usually does not contain the implementation of each function.

In C, #include <stdio.h> asks the preprocessor to find the standard input/output header, stdio.h. It declares functions such as puts() and printf(), so the compiler can check how the program uses them. Here is a small program that uses puts():

#include <stdio.h>

int main(void) {
    puts("Hello");
    return 0;
}

The preprocessor handles #include before the compiler checks the C code. The compiler checks calls against the declarations in the header. Later, the linker connects the program's references to an implementation, often in the C library. The header describes an interface; it usually does not contain the implementation of every function. This is why “header not found” is a preprocessing problem, while “undefined reference” usually appears during linking.

Where include files are found

For an angle-bracket include such as #include <stdio.h>, the compiler searches configured include directories, including system locations. A quoted include such as #include "project.h" normally checks the source file's directory first, then the configured include directories. GCC's -I option adds a directory to that search.

Compile and inspect the search paths with:

cc -Wall -Wextra hello.c -o hello
cc -E -v -x c /dev/null

The first command compiles and links the program. The second preprocesses empty C input and prints GCC's configured include search paths. Actual directories and development packages depend on the compiler target and Linux distribution.

Linux kernel header sets

“Linux headers” can mean different files. C library headers describe the library interface. Linux UAPI headers, the user-space API headers, describe interfaces applications can use to access Linux-specific kernel services. The kernel build system exports a cleaned set of these headers. Internal kernel headers are used to build the kernel and are not interchangeable with the exported user-space set.

Building an out-of-tree kernel module is a different task. It generally needs build headers or a prepared kernel build tree that matches the target kernel and its configuration. Copying arbitrary internal headers into a compiler's include path does not replace those files.

Find which stage failed

If a header cannot be found, check its name and capitalization, language mode, compiler target, and active include paths. For a project header outside the standard directories, add that directory with a compiler option and make the include relationship clear. Copying files into a global include directory can shadow supported headers and affect unrelated programs.

If declarations are found but linking fails, check which library provides the implementation and whether the needed linker flags are present. Knowing whether an error came from preprocessing, compilation, or linking narrows down the cause.

Interactive exercises

Materials

Skillsets
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Devops preview
image descriptionDevops
by Dio
3 users
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful