简体   繁体   English

我编译Linux内核时#include如何工作

[英]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. 我需要使用arm-linux-gcc作为嵌入式系统编译2.6.28 linux内核。我正在运行Ubuntu 12.10 x86。 I viewed the 2.6 kernel source code and found this: 我查看了2.6内核源代码,发现了这一点:

#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? gcc编译器会从/ usr / include / usr / local / include 还是从Linux_2.6.28源文件夹包含这些文件?

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. 正如我在评论中所建议的那样,只需看一眼主要的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 : 一点点ctrl+f和“ include”,这是我可以反馈给您的有趣引号:

# 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)

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. 这些文件不应在/ usr / local等文件中直接访问。如果是,这是一个问题,因为除非使用与该内核相关的文件,否则您的内核将无法正确构建。 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. 内核使用的文件位于linux/include/...目录中。 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. 编译器选项使用-nostdinc避免搜索标准包含位置,然后从内核源目录中添加适当的位置。

To find out what files are included for some given compilation, pass -H to gcc . 要找出某些给定编译包含的文件, -H传递给gcc

To add a directory for searching included files, pass -I somedir to gcc , eg -I /usr/local/include/ ; 要添加目录来搜索包含的文件, 请将 -I somedir传递给gcc ,例如-I /usr/local/include/ ; there are preprocessor options to remove directories or to clear the include path. 预处理器选项可删除目录或清除包含路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM