What's The Difference Between /Usr/Include/Linux and The Include Folder in Linux Kernel Source

What's the difference between /usr/include/linux and the include folder in linux kernel source?

They are very different; the /usr/include/linux headers are the headers that were used when compiling the system's standard C library. They are owned by the C library packaging, and updated in lockstep with the standard C library. They exist to provide the userland interface to the kernel, as understood and "brokered"1 by the C library.

The /usr/src/linux-headers-$(uname -r)/include/linux headers are used via the /lib/modules/$(uname -r)/build symbolic links. They are owned by the kernel headers packages and updated in lockstep with the kernel. These are a subset of the kernel headers and enough of the Kbuild system required to build out-of-tree kernel modules. These files represent the kernel internals -- modules must build against these if they are to properly understand in-memory objects. See the kernel's Documentation/kbuild/modules.txt file for some details.


1: "Mediated" was my first word choice, but it implies some sort of access controls, which isn't the case. "Brokered" implies a third-party process, but that is also not the case. Consider: when a C program calls _exit(), it is actually calling the Standard C library's _exit() wrapper, which calls the exit(2) system call. The select(2) interface has an upper limit on the number of file descriptors that can be tracked, and that limit is compiled into the standard C library. Even if the kernel's interface were extended, the C library would also need to be recompiled.

What's in include/uapi of kernel source project

The uapi folder is supposed to contain the user space API of the kernel. Then upon kernel installation, the uapi include files become the top level /usr/include/linux/ files. (I'm not entirely clear on what exceptions remain.)

The other headers in theory are then private to the kernel. This allow clean separation of the user-visible and kernel-only structures which previously were intermingled in a single header file.

The best discussion I have seen of this is located at a Linux Weekly News article that predates the patch landing.

The UAPI patch itself landed with kernel 3.7. Linus's quick and dirty summary is:

  • the "uapi" include file cleanups. The idea is that the stuff
    exported to user space should now be found under include/uapi and
    arch/$(ARCH)/include/uapi.

    Let's hope it actually works. Because otherwise this was just a
    totally pointless pain in the *ss. And regardless, I'm definitely done
    with these kinds of "let's do massive cleanup of the include files"
    forever.

where are the head file in /usr/include from? Linux kernel code Or Gcc

They both come from GlibC; usually kernel header files are in /usr/include/linux and other subdirs, while gcc includes are located elsewhere (like /usr/lib/<arch>/<version>/include)

asm vs asm-generic in linux headers -- are they same

Short answer

They are not the same.

kernel developer might include asm-generic headers in a asm header while asm headers are the headers required for kernel modules.

You may get more info from following post

  • in linux kernel, asm or asm-generic?
  • Linux kernel headers' organization
  • linux module compilng missed folder asm

Take this question in another way.

It seems you're trying to make a kernel module.

To build a kernel module you need kernel-headers or compiled kernel source code. However I don't know kali linux, so I just provide generic suggestions here.

Where to get them

  • Some of distributions, like Ubuntu, have prebuilt linux-headers.

    • Eg: Ubuntu has it in /usr/src/linux-headers-$(uname -r)/include
  • Download it by sudo apt-get install linux-headers-$(uname -r)

    • It seems kali linux 2.0 might need more operations. Found this post might help.
  • Build it yourself

    1. Checkout linux kernel code of your desired distro.
    2. Set up kernel config with make menuconfig ( You might get stumbled here a while.. many packages might be required )
    3. Compile kernel with make modules_prepare to compile essential Module.symvers for drivers. It take significant less time than compiling a full kernel.

I presume you already found a kernel module build example. If not, you may consult offical kernel module documentation. It helps a lot if you take a while to read first two chapters.

Or another example

Where are include files stored - Ubuntu Linux, GCC

See here: Search Path

Summary:

#include <stdio.h>

When the include file is in brackets the preprocessor first searches in paths specified via the -I flag. Then it searches the standard include paths (see the above link, and use the -v flag to test on your system).

#include "myFile.h"

When the include file is in quotes the preprocessor first searches in the current directory, then paths specified by -iquote, then -I paths, then the standard paths.

-nostdinc can be used to prevent the preprocessor from searching the standard paths at all.

Environment variables can also be used to add search paths.

When compiling if you use the -v flag you can see the search paths used.

Linux kernel headers

I've been looking into this matter a little bit myself recently.

I don't know how related this answer is since it sounds like you are only concerned about understanding the packaging of the kernel source you have on hand. This answer probably only pertains to your second question. Nevertheless here is some stuff I've found about kernel headers.

From what I've found there are two potential locations that end up being referred to as "kernel headers".

The first location is in /usr/src/linux-headers-`version' (at least on my Ubuntu machine). This is where your kernel (source?) is installed as well as the accompanying headers. If you want to build kernel modules then you will need to build against the headers found here.

On the other hand /usr/include/{linux,asm} also contain "kernel headers". You can think of these header files as the userland interface to your kernel. It is the API exported by the kernel so userland programs can make system calls. Your libc will take advantage of this API so it can create functions (in /usr/include) based on which system calls are available.

Here are a couple of documents to back up what I've just said and to clarify things a bit more:

Here is a link about the difference between /usr/src/linux-headers-`version' and /usr/include on RHEL4.

http://www.linuxquestions.org/questions/red-hat-31/rhel4-kernel-devel-headers-packages-missing-directories-697552/

Here is a link about 'exporting' kernel headers:
http://www.kernel.org/doc/Documentation/make/headers_install.txt

Finally here is another explanation of what kernel headers are for:
http://www.overclockers.com/forums/showthread.php?t=647638

So maybe you knew this already and you just wanted to know how to create a driver using the sources in your kernel package, but at least with this answer you know that its definitely the place to start.

How to include the asm header directory for Linux kernel development in Eclipse?

As I have discovered, there are many pieces required to direct Eclipse to index similarly to the kernel build process. However, the answer to this specific question was fairly simple:

Assuming your Linux kernel build directory is defined as ${KDIR}, and your kernel architecture is ${ARCH}, then you need to add the following include paths to your project:

  • ${KDIR}/include
  • ${KDIR}/arch/${ARCH}/include

You can do this in the Project Explorer, by right clicking the project, then Properties > C/C++ General > Paths and Symbols > Includes (tab) > Add ... (button).

I was missing the second entry. Adding it resolved this question. With these 2 entries, checking for unresolved includes (Right click Project > Index > Search for Unresolved Includes) produced 0 errors.

Now I have hit another stumbling block. Some of the types (like, u32 and bool) are still undefined in Eclipse. (My Makefile does not produce any errors.) I believe this is related to some kernel specific variables being undefined in the Eclipse header parsing, causing the include's IFDEF's to not be evaluated the same as during the kernel module compilation. But, I have not resolved this yet, and that pertains to another question. :)

#include-ing certain header files in old C source (page.h et al) on Ubuntu

There are two different sets of header files: one for Linux kernel programming, the other for general user-space programs.

/usr/src/linux* is for the kernel stuff.

/usr/include/* are the "normal" headers.

You do not want to mix'n'match the two.

If you're programming a game, I'd strongly encourage you to look at SDL or OpenGL:

  • http://www.linuxjournal.com/article/6410

  • http://content.gpwiki.org/index.php/SDL:Tutorials

  • http://www.opengl.org/wiki/Getting_Started

  • http://www.dreamincode.net/forums/topic/63945-game-programming-in-linux-for-windows-programmers-part-1/

ADDENDUM:

  1. The problem is you're missing the fbdev (user-space) headers. You do NOT want to bring in any Linux kernel source headers.

  2. You should be able to get the header with this command:

    sudo apt-get xserver-xorg-video-fbdev

  3. You might need to bring in other dependencies as well - apt-get should tell you.

'Hope that helps .. PSM

PS:

It appears that your book, "Programming Linux Games" is also a well-regarded tutorial on SDL.



Related Topics



Leave a reply



Submit