Wednesday, April 22, 2009

Kernel Headers explained

Thanks to BrionS at USA Linux Users Group for this info:

Put simply headers are sets of files that define all the application programming interface (API) for a program. So if you're writing some C or C++ code and you want to use the function: getWidget(myContext); Then somewhere in your build path (when you go to compile the code) you need a header that describes where getWidget(Context c) is defined.

Header files end in .h or sometimes .h++. Source code files typically end in .c, .cpp, or .c++ and inside them you'll see references to .h files (sometimes - it can get complicated).

In any event, to compile a program that uses what's called a function library (or just library), you need to have the header files for that library so your compiler can check your code for syntax problems (by comparing what you wrote to the function definition in the header(s)). You don't need the entire source code for the library in order to use the functions - this saves you a lot of space because header files are very small compared to the source files.

You could think of header files like Cliff Notes. Smile Your program needs to know a little bit of information about one of the functions it's using, the header files provide a very brief overview of the function which is enough for you to compile. If you wanted the whole story, you'd need the function's source code.

The reason the header files are not generally included by default is because these days most distributions provide you with binary (pre-compiled) packages for your system. Since you don't have to do any compiling, you don't need to look up any function definitions by their headers, you can simply run your software which has already been verified against the headers when it was compiled and it uses the pre-compiled function libraries (lib*.so) on your system.

If you have the wrong version of a function library on your system, then the pre-compiled binary usually breaks with a segmentation fault or something like that and you either need to get the right library version installed or re-compile the program from source using the library version (and its headers) that you have so it is now referencing the correct API.

Lots of words above, but it all boils down to this:

1. If you install binary packages, you don't need header files

2. If you install source packages, you'll need the header files and/or source of that package's dependencies

No comments: