如何编写一个简单的 Linux 内核模块
1个回答
展开全部
编写helloworld.c及其对应的Makefile。
helloworld.c:
#include <linux/module.h>#include <linux/kernel.h>int init_hello_module(void)
{
printk("***************Start***************\n");
printk("Hello World! Start of hello world module!\n"); return 0;
}void exit_hello_module(void)
{
printk("***************End***************\n");
printk("Hello World! End of hello world module!\n");
}
MODULE_LICENSE("Dual BSD/GPL");
module_init(init_hello_module);
module_exit(exit_hello_module);1234567891011121314151617181920
Makefile:
# To build modules outside of the kernel tree, we run "make"# in the kernel source tree; the Makefile these then includes this# Makefile once again.# This conditional selects whether we are being included from the# kernel Makefile or not.# called from kernel build system: just declare what our modules areobj-m := helloworld.oCROSS_COMPILE =
CC = gcc # Assume the source tree is where the running kernel was built
# You should set KERNELDIR in the environment if it's elsewhere
KERNELDIR ?= /usr/src/linux-headers-$(shell uname -r) # The current directory is passed to sub-makes as argument
PWD := $(shell pwd)all: modulesmodules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modulesclean:
rm -rf *.o *~ core .depend *.symvers .*.cmd *.ko *.mod.c .tmp_versions $(TARGET)
在Makefile中,在obj-m := helloworld.o这句中,.o的文件名要与编译的.c文件名一致。
KERNELDIR ?= /usr/src/linux-headers-$(shell uname -r)指示当前linux系统内核的源码位置。
helloworld.c:
#include <linux/module.h>#include <linux/kernel.h>int init_hello_module(void)
{
printk("***************Start***************\n");
printk("Hello World! Start of hello world module!\n"); return 0;
}void exit_hello_module(void)
{
printk("***************End***************\n");
printk("Hello World! End of hello world module!\n");
}
MODULE_LICENSE("Dual BSD/GPL");
module_init(init_hello_module);
module_exit(exit_hello_module);1234567891011121314151617181920
Makefile:
# To build modules outside of the kernel tree, we run "make"# in the kernel source tree; the Makefile these then includes this# Makefile once again.# This conditional selects whether we are being included from the# kernel Makefile or not.# called from kernel build system: just declare what our modules areobj-m := helloworld.oCROSS_COMPILE =
CC = gcc # Assume the source tree is where the running kernel was built
# You should set KERNELDIR in the environment if it's elsewhere
KERNELDIR ?= /usr/src/linux-headers-$(shell uname -r) # The current directory is passed to sub-makes as argument
PWD := $(shell pwd)all: modulesmodules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modulesclean:
rm -rf *.o *~ core .depend *.symvers .*.cmd *.ko *.mod.c .tmp_versions $(TARGET)
在Makefile中,在obj-m := helloworld.o这句中,.o的文件名要与编译的.c文件名一致。
KERNELDIR ?= /usr/src/linux-headers-$(shell uname -r)指示当前linux系统内核的源码位置。
力控科技
2024-08-19 广告
2024-08-19 广告
ForceSCADA是力控科技信创产品的重要组成部分,具备完全自主知识产权,支持部署在Linux桌面版、服务器版、嵌入式等系统架构下。使用ForceSCADA可以搭建创新性高、扩展性佳、融合度强的SCADA平台,进而构建高效、智能化的监控中...
点击进入详情页
本回答由力控科技提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询