编译文件与工具学习(二)——尝试Ubuntu24.04开发内核模块-代码+编译

时间:2024-11-07 09:46:09
//testKernel.c
#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/init.h>

static int __init lkp_init(void)
{
	printk("<1>Hello, World! from the kernel space...\n");
	return 0;
}

static void __exit lkp_cleanup(void)
{
	printk("<1>Good Bye, World! leaving kernel space...\n");
}

module_init(lkp_init);	// 注册模块
module_exit(lkp_cleanup);	// 注销模块
MODULE_LICENSE("GPL"); 	//告诉内核该模块具有GNU公共许可证
# Makefile 4.0
obj-m := testKernel.o
CURRENT_PATH := $(shell pwd)
LINUX_KERNEL := $(shell uname -r)
LINUX_KERNEL_PATH := /usr/src/linux-headers-$(LINUX_KERNEL)

all:
	make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) modules
clean:
	make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) clean

编译文件

make CC=gcc-13 CXX=g++-13

插入系统内核

sudo insmod testKernel.ko

执行失败:

insmod: ERROR: could not insert module testKernel.ko: Key was rejected by service