You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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.

Your Linux Kernel Hello World Module: Code Fixes & Proper Compilation/Running Guide

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_LICENSE immediately: 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 printk note: Unlike Windows driver debug output that goes to tools like DbgView, printk writes to the kernel log—you’ll need to use dmesg or journalctl to 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 kernel
  • M=$(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 -r to check, and install the linux-headers-$(uname -r) package if needed)
  • insmod is like manually loading a driver, while modprobe handles dependencies automatically—your Hello World module has no dependencies, so insmod works fine
  • Always test modules on a development machine or virtual machine, not production systems

内容的提问来源于stack exchange,提问作者Dhyan Deep A.K

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:59:44