How to write Makefile for kernel modules

本文介绍了一个用于构建Linux内核模块的Makefile示例。通过条件判断KERNELRELEASE是否存在来决定构建流程,若不存在,则通过一系列命令完成内核模块的构建。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ifneq ($(KERNELRELEASE),)                    
obj-m := mymodule.o                                 
mymodule-objs := file_one.o file_two.o      
                                                                   
                                                                   
else
KDIR := /lib/modules/$(shell uname -r)/build     
PWD := $(shell pwd)                                           
default:
       $(MAKE) -C $(KDIR) M=$(PWD) modules  
clean:
       rm -rf .*.cmd *.o *.mod.c *.ko .tmp_versions
endif


1.  Reading the makefile and then find that the KERNELRELEASE haven't defined because KERNELRELEASE equal null at firstly
2.  Execute  commands in the else scope, then come to the $(MAKE) -C $(KDIR) M=$(PWD) modules
3.  Then change directory into $(KDIR),read the makefile in this path ($KDIR),we called it kbuild
4.  Return to the current path $(PWD)
5.  Kbuild is started and read makefile again specificted by M=$(PWD). KERNELRELEASE is defined now for kbuild have maked it.
6.  Then come to the obj-m and my-module-objs setting, and kbuild finishes the remain works, creating the kernel modules .

How is emlog used? 1: Configure, compile, and install emlog If you want to compile emlog for use with the currently running kernel, simply run make Otherwise, you have to set either KVER (for linux kernel sources, located in /lib/modules/<KVER>/build) or KDIR (for any other path): make KDIR=/usr/src/linux Two files should be generated: the kernel module itself (emlog.ko), and the nbcat utility that will be described later. You can use them directly from the current directory or you can install them via make install 2: Load the emlog module into the kernel If you chose to use emlog directly from the current directly, insert the module into the kernel using the insmod command insmod emlog.ko Otherwise, modprobe should work: modprobe emlog To specify a different maximum buffer size limit: modprobe emlog emlog_max_size=2048 If successful, a message similar to emlog:emlog_init: version 0.70 running, major is 251, MINOR is 1, max size 1024 K. should show up in your kernel log (type dmesg to see it). You can also verify that the module has been inserted by typing lsmod or cat /proc/modules. 3: Create device files for emlog By default, a device file /dev/emlog is created (if you have devtmpfs mounted onto /dev and/or have udev running) with a minimal allocated buffer. It's ready to be written to/read from. If you need more devices/buffers, you should can use the mkemlog program to create device files that your processes can write to. Usage mkemlog <logdevname> [size_in_kilobytes] [mode] 3.1: Examples usage mkemlog Create a log file with a 8k buffer with file permissions 0660 mkemlog /tmp/testlog Create a log file with a 17k buffer with file permissions 0660 mkemlog /tmp/testlog 17 Create a log file with a 12k buffer with file permissions 0644 mkemlog /tmp/testlog_12k 12 0644 Create a log file with a 18k buffer with file permissions 0644, owned by a user with UID==1000. mkemlog /tmp/testlog_18k 18 0644 1000 The mkemlog requires the /dev/emlog file to be cemlog
04-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值