The Linux kernel source code contains numerous files, and understanding the relationships between Makefile, Kconfig, and .config is crucial for navigating the kernel compilation system. Issues arise when trying to compile and modify the kernel, integrate your own drivers, or configure the kernel. All of these problems relate to Makefile, Kconfig, and .config. Below, I will briefly discuss Makefile, Kconfig, and .config. I hope this will inspire you.
1. The Role of Each
In simple terms, think of it like ordering from a restaurant: Kconfig is the menu, Makefile is the recipe, and .config is your order.
Makefile: A text file that defines how to compile source files.
Kconfig: A text file that serves as the kernel’s configuration menu.
.config: The configuration file upon which the kernel compilation is based.
2. The Syntax of Each
1. Makefile
Reference: linux-3.4.2/drivers/Makefile
Role: Defines what content is compiled as a module and under what conditions. The subdirectory Makefile is included by the top-level Makefile.
(1) Direct Compilation
obj-y += xxx.o
This indicates that xxx.c or xxx.s is compiled into xxx.o and included directly in the kernel.
(2) Conditional Compilation
obj -$(CONFIG_HELLO) += xxx.o
This determines whether the file is included in the kernel based on the CONFIG_XXX in the .config file.
(3) Module Compilation
obj-m +=xxx.o
This indicates that xxx is compiled as a module, meaning it will only be compiled when make modules is executed.
2. Kconfig
Each config menu item has a type definition: bool for boolean type, tristate for three states (built-in, module, removed), string for strings, hex for hexadecimal, integer for integers.
Role: Determines the menu items displayed during make menuconfig.
Reference: linux-3.4.2/drivers/leds/Kconfig:
config LEDS_S3C24XX
tristate “LED Support for Samsung S3C24XX GPIO LEDs”
depends on LEDS_CLASS
depends on ARCH_S3C24XX
help
This option enables support for LEDs connected to GPIO lines
on Samsung S3C24XX series CPUs, such as the S3C2410 and S3C2440.
LEDS_S3C24XX: The name of the configuration option, omitting the prefix “CONFIG_”.
Tristate:
bool:
dependon:
select:
help:
Directory Hierarchy Iteration:
3. .config
Reference: linux-3.4.2/.config
Through the analysis of the previous two files, the meaning of .config is clear: it is a reference file for kernel compilation, and by examining its contents, you can determine which drivers are compiled into the kernel.
There are three ways to configure the kernel (choose one):
(1) make menuconfig
(2) make xxx_defconfig
(3) Directly modify .config
Note: If you directly modify .config, it may not take effect, as some configurations may have dependencies. During make, the rules will be checked based on these dependencies, and direct modifications to .config may sometimes be ineffective, so direct modification is not recommended.
While the above may seem abstract, let me illustrate with an example:
Write a simple entry function that outputs hello world as a driver and compile it into the kernel.
Steps:
(1) Create a hello folder under the drivers directory, implementing hello.c, Makefile, and Kconfig inside.
hello.c:
#include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> static int first_drv_init(void) { printk("------------------hello world !--------------------"); return 0; } static void first_drv_exit(void) { printk("------------------exit hello world !--------------------"); } module_init(first_drv_init); module_exit(first_drv_exit); MODULE_LICENSE("GPL");
Makefile:
obj-$(CONFIG_HELLO) += hello.o
Kconfig:
config HELLO
tristate “Hello World for fengyuwuzu”
help
Hello for fengyuwuzu
The config HELLO determines the name: CONFIG_HELLO.
The Hello World for fengyuwuzu determines the name displayed during make menuconfig.
(2) Modify the upper-level (Linux-3.4.2/drivers) Makefile and Kconfig.
Makefile:
obj-y += hello/
Kconfig:
source “drivers/hello/Kconfig”
(3) make menuconfig
(4) make uImage and then burn it to the development board.
Check the kernel boot log, and the great hello world appears! This indicates that hello.c has been successfully compiled into the kernel.
Feel free to refer to this experiment; I believe you will gain a clearer understanding of the kernel’s organizational structure.
Finally
The little brother has collected some embedded learning materials; reply with【1024】 in the public account to find the download link!
Recommended good articles Click the blue text to jump to it ☞ Collection | Linux Application Programming Complete ☞ Collection | Learn Some Network Knowledge ☞ Collection | Handwritten C Language ☞ Collection | Handwritten C++ Language ☞ Collection | Experience Sharing ☞ Collection | From Microcontrollers to Linux ☞ Collection | Power Control Technology ☞ Collection | Essential Mathematics for Embedded Systems ☞ MCU Advanced Collection ☞ Embedded C Language Advanced Collection ☞ Experience Sharing