What are linux headers

Asked by Dines683 on 2008-12-11

This is question due to simple curiosity ..

Can anyone tell me what kernel headers are ?

The reason from me asking this question is, that i have just installed virtualbox(downloaded from the virtual box home page, not he OSE version). I, however, did have some installation problems. The problems were solved by installing the linux headers:

sudo apt-get install linux-headers-`uname -r`

and then recompile the kernel module:

sudo /etc/init.d/vboxdrv setup

So now i am curious about what the linux headers are?

Question information

Language:
English Edit question
Status:
Answered
For:
Ubuntu Edit question
Assignee:
No assignee Edit question
Last query:
2008-12-11
Last reply:
2012-05-28
Dines683 (dines683) said : #1

Before i installed the linux headers i encountered the following problem when trying to compile the kernel module

unable to find the sources of your current Linux kernel. Specify KERN_DIR=<directory> and run Make again.. .

Kernel *headers* are the header files used to compile the kernel - and other
applications which depend on the symbols / structures defined in these
header files, like kernel modules. An example can be graphic card drivers;
if the driver does not have a binary matching the running kernel and needs
to be compiled.

No trees were harmed in creating this message.
However, a large number of electrons were terribly inconvenienced.

On Thu, Dec 11, 2008 at 4:36 PM, Dines683 <
<email address hidden>> wrote:

> New question #54157 on Ubuntu:
> https://answers.launchpad.net/ubuntu/+question/54157
>
> This is question due to simple curiosity ..
>
> Can anyone tell me what kernel headers are ?
>
> The reason from me asking this question is, that i have just installed
> virtualbox(downloaded from the virtual box home page, not he OSE version).
> I, however, did have some installation problems. The problems were solved by
> installing the linux headers:
>
> sudo apt-get install linux-headers-`uname -r`
>
> and then recompile the kernel module:
>
> sudo /etc/init.d/vboxdrv setup
>
> So now i am curious about what the linux headers are?
>
> --
> You received this question notification because you are an answer
> contact for Ubuntu.
>

Since this answer isn't really complete, yet has managed to make it's way to the front page of my google search, I figured I'd add a much more complete answer. Credit to BrionS @ usa linux user group here: http://usalug.com/phpBB3/viewtopic.php?t=12083 for one of the best explanations I've seen.

* * * *

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. 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 (NVidia is an exception that I'll explain)
2. If you install source packages, you'll need the header files and/or source of that package's dependencies

NVidia is different because what they do is provide a binary chunk of proprietary code (pre-compiled but not runnable without some additional system-specific code) wrapped in some open source code that you must compile for your machine in order to use the driver. This way, NVidia can use the same core code for their Windows and Linux drivers, and just wrap the core in platform-specific code that hooks the driver into the OS. That's why installing the NVidia proprietary driver needs the kernel headers to be installed in order to compile (but the driver core itself is still proprietary and is actually pre-compiled platform-independent data).

Hope that helps more than it confuses.

P.S. One other thing to keep in mind is that the kernel header packages range in name from 'linux-headers' to 'kernel-headers' and other variations that end in '-headers'. However, most other packages keep their headers in a *-devel[op] or *-dev package. So if you need the Pidgin development headers to compile your own plugin, you'd need to install the libpurple-dev package (libpurple is the Pidgin function library), but the headers for KDE are found in the kde-devel package, and finally the headers for H.323 VoIP library is libopenh323-*-develop.

P.P.S. Source code may be found in the development package or it may be in its own source package (usually *-source) such as sun-java6-source which contains the Sun Java Development Kit (JDK) 6 source files.

Can you help with this problem?

Provide an answer of your own, or ask Dines683 for more information if necessary.

To post a message you must log in.