Michael Zhang
Michael Zhang

Reputation: 43

how #Include works when I compile a linux kernel

I need to compile a 2.6.28 linux kernel with arm-linux-gcc as an embeded system.I'm running Ubuntu 12.10 x86. I viewed the 2.6 kernel source code and found this:

#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/input.h>
#include <asm/io.h>
#include <asm/irq.h>
...

Will gcc compiler include these files from /usr/include /usr/local/include or from Linux_2.6.28 source folder?

Upvotes: 2

Views: 1452

Answers (3)

Rerito
Rerito

Reputation: 6086

The Kernel is self-contained. This means that it is not allowed to have any external dependency. In other words, your Kernel source tree contains all the material needed to build your Kernel. There is no point to look for code anywhere else.

As I suggested in my comments, just take a glance at the main Makefile. You'll find it under the root of your source tree. A little ctrl+f with "include" and here's interesting quotes I can feed back to you :

# Look for make include files relative to root of kernel src
MAKEFLAGS += --include-dir=$(srctree)
# .... Other stuff
# Use USERINCLUDE when you must reference the UAPI directories only.
USERINCLUDE    := \
    -I$(srctree)/arch/$(hdr-arch)/include/uapi \
    -Iarch/$(hdr-arch)/include/generated/uapi \
    -I$(srctree)/include/uapi \
    -Iinclude/generated/uapi \
    -include $(srctree)/include/linux/kconfig.h

# Use LINUXINCLUDE when you must reference the include/ directory.
# Needed to be compatible with the O= option
LINUXINCLUDE    := \
    -I$(srctree)/arch/$(hdr-arch)/include \
    -Iarch/$(hdr-arch)/include/generated \
    $(if $(KBUILD_SRC), -I$(srctree)/include) \
    -Iinclude \
    $(USERINCLUDE)

Upvotes: 7

To find out what files are included for some given compilation, pass -H to gcc.

To add a directory for searching included files, pass -I somedir to gcc, e.g. -I /usr/local/include/; there are preprocessor options to remove directories or to clear the include path.

Upvotes: 2

Mats Petersson
Mats Petersson

Reputation: 129374

These files should not be directly accessible in the /usr/local etc. If they are, it's a problem, because your kernel will not build correctly unless it uses the ones that belong to that kernel. Some of these files change on a regular basis, as the kernel is being updated and improved.

The files used by the kernel are found in the linux/include/... directory. The compiler options use -nostdinc to avoid the standard include locations from being searched, and then add the appropriate locations from within the kernel source directory.

Upvotes: 3

Related Questions