Home | Projects | Notes > Linux Device Drivers > Linux Kernel Module Build - In-Tree Module
Add the LKM inside the Linux kernel source tree and let the Linux build system build it.
To list your kernel module selection in kernel menuconfig, create and use a Kconfig file.
Create a directory in linux_bbb_4.14/drivers/char/my_c_dev/
Copy main.c
Create Kconfig file and add the following entries:
xxxxxxxxxx51menu "Kyungjae's custom modules"2    config CUSTOM_HELLOWORLD3        tristate "helloworld module support"4        default n5endmenu
CUSTOM_HELLOWORLDis an identifier by which the kernel identifies your custom module.User custom module can be selected or unselected. If selected, it can be either static module or dynamic module.
Each kernel module has 3 states;
y,m,n. You can specify it by using thetristateanddefaultkeyword.
n- Unselected by default
y- Select by default
Add the local Kconfig entry to upper-level Kconfig:
Go one level up (i.e., linux_bbb_4.14/drivers/char/), open Kconfig and add the following line at the end:
xxxxxxxxxx11source "drivers/char/my_c_dev/Kconfig"Create a local Makefile in linux_bbb_4.14/drivers/char/my_c_dev/.
Add obj-<config_item> += <module>.o to the local Makefile
config_item- The custom module identifier (e.g.,CUSTOM_HELLOWORLD)
xxxxxxxxxx11obj-$(CONFIG_CUSTOM_HELLOWORLD) += main.oSince the state of this module will be selected via menu, we cannot specify it at the time of writing the Makefile.
$(CONFIG_CUSTOM_HELLOWORLD)will be replaced by the selected state.
Add the local level Makefile to higher level Makefile:
Go one level up (i.e., linux_bbb_4.14/drivers/char/), open the Makefile and add the following line at the end:
xxxxxxxxxx11obj-y += my_c_dev/This is how you direct the higher-level Makefile to run another Makefile.
-ysince you wantmy_c_devdirectory always selected.
Run the Kernel Configuration:
xxxxxxxxxx11make ARCH=arm menuconfigSelect M for helloworld module support and exit saving your new configuration!



Open the updated linux_bbb_4.14/.config file and search for CONFIG_CUSTOM_HELLOWORLD.
If it is there, it means that your custom module is now part of the kernel source tree.

Build the kernel modules!
In the Linux kernel source directory linux_bbb_4.14/ run:
xxxxxxxxxx11make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- modules -j4You'll be able to see
main.kogenerated.
Check the module info and see if the built module is marked as "intree":
Go to linux_bbb_4.14/drivers/char/my_c_dev and run:
xxxxxxxxxx11modinfo main.koThen, you'll see the intree: field is marked Y.
[!] Reference: https://www.kernel.org/doc/Documentation/kbuild/kconfig-language.txt