Kernel modules are usually built out-of-tree, but for modules tightly coupled with kernel internals it's better to build them in-tree, as part of the kernel compilation itself, for header and config compatibility and automatic compilation during make . The example below uses linux-5.10/ as the source tree. Step 1: Create the Module Source Create the module source under drivers/hello_world/ : Step 2: Create the Module Makefile Create drivers/hello_world/Makefile : Step 3: Register the Module in the Build System Register the module in drivers/Makefile (append): And add this to drivers/Kconfig : Step 4: Enable the Module in Kernel Config Navigate to Device Drivers -> Hello World Module -> Build Hello Kernel Module Select M for module Step 5: Compile Or just modules: This produces hello.ko inside drivers/hello_world/ when CONFIG_HELLO_MODULE=m .
Architecture and Core Concepts
Step 6: Install and Test Or manually: Check with: Remove with: Common Mistakes Missing Makefile/obj line: the kernel won't see the module. Skipping Kconfig registration: the module isn't selectable via menuconfig. Wrong file permissions: causes build errors. Misusing printk: can cause kernel panic or missed logs. Best Practices Keep modules in their own subfolder.
Execution Syntax and Code Implementation
sed logsmake once CONFIG_HELLO_MODULE is enabledGotchas and Troubleshooting Checklist
Permission & Access Control - verify user privilege level (sudo access or file ownership) before running commands.
Environment & Path Resolution - confirm environment variables and binary PATH entries match target software releases.
Log Diagnostics - inspect relevant system logs or application trace outputs to verify clean execution.
Following these steps provides a clean, reliable, and production-ready solution for building kernel modules in-tree during kernel compilation.
Comments and corrections