2023-07-08 Understanding Linux Kernel Module Basic Concepts

#include <linux/module.h>

#include <linux/init.h>

static int __init helloworld_init(void) {

pr_info("Hello world initialization!\n");

return 0;

}

static void __exit helloworld_exit(void) {

pr_info("Hello world exit\n");

}

module_init(helloworld_init);

module_exit(helloworld_exit);

MODULE_LICENSE("GPL");                               // GPL (General Public License)

MODULE_AUTHOR("John Madieu <john.madieu@gmail.com>");

MODULE_DESCRIPTION("Linux kernel module skeleton");

模块骨架代码

模块加载:modprobe or insmod  卸载:rmmod or  modprobe -r

MODULE_INFO(my_field_name, "What easy value");


kbuild   Kconfig   Makefile

内核模块构建命令模式类似于如下: make -C $KERNEL_SRC M=$(shellpwd)[目标]          

Makefile:

# kbuild part of makefile

obj-m := helloworld.o

#the following is just an example

#ldflags-y := -T foo_sections.lds

# normal makefile

KERNEL_SRC ?= /lib/modules/$(shell uname -r)/build

all default: modules

install: modules_install

modules modules_install help clean:

$(MAKE) -C $(KERNEL_SRC) M=$(shell pwd) $@


报错 Makefile:10: *** missing separator. Stop.  第十行命令必须以tab键开头,解决



Out-of-tree module cross-compiling

https://blog.csdn.net/Tdh5258/article/details/108501265    可以参考!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容