Windows驱动开发者入门Linux内核:HelloWorld模块开发咨询
Hey there! As someone who’s made the jump from Windows driver development to Linux kernel modules, I totally get where you’re starting from. Let’s walk through fixing your code and getting it up and running properly.
Code Adjustments You Need to Make
Your core code structure is solid, but there are a couple critical tweaks and nice-to-have improvements:
- Uncomment
MODULE_LICENSEimmediately: This line is non-negotiable! The Linux kernel requires all modules to declare a license; leaving it commented will mark your module as "tainted" (a flag for untrusted/unsupported code) and trigger warning logs. Fix it like this:MODULE_LICENSE("Dual BSD/GPL"); - Add optional (but recommended) metadata: Including module details makes your code more maintainable and follows kernel community standards:
MODULE_DESCRIPTION("Simple Hello World Linux Kernel Module"); MODULE_AUTHOR("Your Name"); - Quick
printknote: Unlike Windows driver debug output that goes to tools like DbgView,printkwrites to the kernel log—you’ll need to usedmesgorjournalctlto see your messages, not the terminal directly.
Here’s the polished, production-ready version of your code:
#include <linux/init.h> #include <linux/module.h> MODULE_LICENSE("Dual BSD/GPL"); MODULE_DESCRIPTION("Simple Hello World Linux Kernel Module"); MODULE_AUTHOR("Your Name"); static int hello_init(void) { printk(KERN_ALERT "Hello, world\n"); return 0; } static void hello_exit(void) { printk(KERN_ALERT "Goodbye, cruel world\n"); } module_init(hello_init); module_exit(hello_exit);
Proper Compilation & Running Steps
Unlike Windows driver development (where you might use MSVC + WDK), Linux kernel modules can’t be compiled with regular gcc—you need to use the kernel’s built-in Kbuild system to ensure compatibility with your running kernel’s configuration and headers.
1. Create a Makefile
In the same directory as your hello.c file, create a capitalized Makefile (case matters on Linux) with this content:
# Declare the module to build: obj-m += <module-name>.o obj-m += hello.o # Compile target: Invoke the kernel's Makefile all: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules # Clean up compiled files clean: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
-C /lib/modules/$(shell uname -r)/build: Switches to the build directory of your currently running kernelM=$(PWD): Tells the kernel’s Makefile to use your current directory for source files
2. Compile the Module
Open a terminal, navigate to your code directory, and run:
make
If successful, you’ll see a hello.ko file—this is your compiled kernel module.
3. Load the Module
You need root privileges to load kernel modules. Run:
sudo insmod hello.ko
To see your "Hello, world" message, check the kernel log:
# Using dmesg dmesg | tail -n 5 # Or on systemd-based systems journalctl -k -n 5
4. Unload the Module
Unload the module with root privileges:
sudo rmmod hello
Check the kernel log again, and you’ll see the "Goodbye, cruel world" message.
Quick Tips for Windows Driver Devs New to Linux
- Make sure your installed kernel headers match your running kernel version (use
uname -rto check, and install thelinux-headers-$(uname -r)package if needed) insmodis like manually loading a driver, whilemodprobehandles dependencies automatically—your Hello World module has no dependencies, soinsmodworks fine- Always test modules on a development machine or virtual machine, not production systems
内容的提问来源于stack exchange,提问作者Dhyan Deep A.K

