Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how #Include works when I compile a linux kernel

Tags:

c

linux

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?

like image 617
Michael Zhang Avatar asked Jun 13 '26 01:06

Michael Zhang


2 Answers

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)
like image 146
Rerito Avatar answered Jun 15 '26 08:06

Rerito


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.

like image 37
Mats Petersson Avatar answered Jun 15 '26 06:06

Mats Petersson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!