Linux .ko模块的生成方式

转载地址:http://blog.csdn.net/tugouxp/article/details/69053633#include "linux/init.h"  
#include "linux/module.h"  
#include "linux/kernel.h"  
//必选  
//模块许可声明  
MODULE_LICENSE("Dual BSD/GPL");  
//模块加载函数  
static int hello_init(void)  
{  
    printk(KERN_ALERT "hello,I am edsionte/n");  
    return 0;  
}  
//模块卸载函数  
static void hello_exit(void)  
{  
    printk(KERN_ALERT "goodbye,kernel/n");  
}  
//模块注册  
module_init(hello_init);  
module_exit(hello_exit);  
//可选  
MODULE_AUTHOR("edsionte Wu");  
MODULE_DESCRIPTION("This is a simple example!/n");  
MODULE_ALIAS("A simplest example");  

Makefile

obj-m += helloworld.o  
#generate the path  
CURRENT_PATH:=$(shell pwd)  
#the current kernel version number  
LINUX_KERNEL:=$(shell uname -r)  
#the absolute path  
LINUX_KERNEL_PATH:=/usr/src/linux-headers-$(LINUX_KERNEL)  
#complie object  
all:  
    make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) modules  
#clean  
clean:  
    make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) clean  
make -C /usr/src/linux-headers-3.19.0-25-generic     M=/home/caozilong/WorkSpace/helloworld   modules  
make[1]: 正在进入目录 `/usr/src/linux-headers-3.19.0-25-generic'
test -e include/generated/autoconf.h -a -e include/config/auto.conf || (        \
    echo >&2;                           \
    echo >&2 "  ERROR: Kernel configuration is invalid.";       \
    echo >&2 "         include/generated/autoconf.h or include/config/auto.conf are missing.";\
    echo >&2 "         Run 'make oldconfig && make prepare' on kernel src to fix it.";  \
    echo >&2 ;                          \
    /bin/false)
mkdir -p /home/caozilong/WorkSpace/helloworld/.tmp_versions ; rm -f /home/caozilong/WorkSpace/helloworld/.tmp_versions/*
make -f ./scripts/Makefile.build obj=/home/caozilong/WorkSpace/helloworld
  gcc -Wp,-MD,/home/caozilong/WorkSpace/helloworld/.helloworld.o.d  -nostdinc -isystem /usr/lib/gcc/x86_64-linux-gnu/4.8/include  -I./arch/x86/include -Iarch/x86/include/generated/uapi -Iarch/x86/include/generated  -Iinclude -I./arch/x86/include/uapi -Iarch/x86/include/generated/uapi -I./include/uapi -Iinclude/generated/uapi -include ./include/linux/kconfig.h -Iubuntu/include  -D__KERNEL__ -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -Werror-implicit-function-declaration -Wno-format-security -std=gnu89 -m64 -mno-80387 -mno-fp-ret-in-387 -mtune=generic -mno-red-zone -mcmodel=kernel -funit-at-a-time -maccumulate-outgoing-args -DCONFIG_X86_X32_ABI -DCONFIG_AS_CFI=1 -DCONFIG_AS_CFI_SIGNAL_FRAME=1 -DCONFIG_AS_CFI_SECTIONS=1 -DCONFIG_AS_FXSAVEQ=1 -DCONFIG_AS_CRC32=1 -DCONFIG_AS_AVX=1 -DCONFIG_AS_AVX2=1 -pipe -Wno-sign-compare -fno-asynchronous-unwind-tables -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -mno-avx -fno-delete-null-pointer-checks -O2 --param=allow-store-data-races=0 -Wframe-larger-than=1024 -fstack-protector -Wno-unused-but-set-variable -fno-omit-frame-pointer -fno-optimize-sibling-calls -fno-var-tracking-assignments -pg -mfentry -DCC_USING_FENTRY -Wdeclaration-after-statement -Wno-pointer-sign -fno-strict-overflow -fconserve-stack -Werror=implicit-int -Werror=strict-prototypes -DCC_HAVE_ASM_GOTO  -DMODULE  -D"KBUILD_STR(s)=#s" -D"KBUILD_BASENAME=KBUILD_STR(helloworld)"  -D"KBUILD_MODNAME=KBUILD_STR(helloworld)" -c -o /home/caozilong/WorkSpace/helloworld/.tmp_helloworld.o /home/caozilong/WorkSpace/helloworld/helloworld.c
  if [ "-pg" = "-pg" ]; then if [ /home/caozilong/WorkSpace/helloworld/helloworld.o != "scripts/mod/empty.o" ]; then ./scripts/recordmcount  "/home/caozilong/WorkSpace/helloworld/helloworld.o"; fi; fi;
(cat /dev/null;   echo kernel//home/caozilong/WorkSpace/helloworld/helloworld.ko;) > /home/caozilong/WorkSpace/helloworld/modules.order
make -f ./scripts/Makefile.modpost
  find /home/caozilong/WorkSpace/helloworld/.tmp_versions -name '*.mod' | xargs -r grep -h '\.ko$' | sort -u | sed 's/\.ko$/.o/' | scripts/mod/modpost -m -a -i ./Module.symvers -I /home/caozilong/WorkSpace/helloworld/Module.symvers  -o /home/caozilong/WorkSpace/helloworld/Module.symvers -S -w  -s -T -
  gcc -Wp,-MD,/home/caozilong/WorkSpace/helloworld/.helloworld.mod.o.d  -nostdinc -isystem /usr/lib/gcc/x86_64-linux-gnu/4.8/include  -I./arch/x86/include -Iarch/x86/include/generated/uapi -Iarch/x86/include/generated  -Iinclude -I./arch/x86/include/uapi -Iarch/x86/include/generated/uapi -I./include/uapi -Iinclude/generated/uapi -include ./include/linux/kconfig.h -Iubuntu/include  -D__KERNEL__ -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -Werror-implicit-function-declaration -Wno-format-security -std=gnu89 -m64 -mno-80387 -mno-fp-ret-in-387 -mtune=generic -mno-red-zone -mcmodel=kernel -funit-at-a-time -maccumulate-outgoing-args -DCONFIG_X86_X32_ABI -DCONFIG_AS_CFI=1 -DCONFIG_AS_CFI_SIGNAL_FRAME=1 -DCONFIG_AS_CFI_SECTIONS=1 -DCONFIG_AS_FXSAVEQ=1 -DCONFIG_AS_CRC32=1 -DCONFIG_AS_AVX=1 -DCONFIG_AS_AVX2=1 -pipe -Wno-sign-compare -fno-asynchronous-unwind-tables -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -mno-avx -fno-delete-null-pointer-checks -O2 --param=allow-store-data-races=0 -Wframe-larger-than=1024 -fstack-protector -Wno-unused-but-set-variable -fno-omit-frame-pointer -fno-optimize-sibling-calls -fno-var-tracking-assignments -pg -mfentry -DCC_USING_FENTRY -Wdeclaration-after-statement -Wno-pointer-sign -fno-strict-overflow -fconserve-stack -Werror=implicit-int -Werror=strict-prototypes -DCC_HAVE_ASM_GOTO  -D"KBUILD_STR(s)=#s" -D"KBUILD_BASENAME=KBUILD_STR(helloworld.mod)"  -D"KBUILD_MODNAME=KBUILD_STR(helloworld)" -DMODULE  -c -o /home/caozilong/WorkSpace/helloworld/helloworld.mod.o /home/caozilong/WorkSpace/helloworld/helloworld.mod.c
  **ld -r -m elf_x86_64 -T ./scripts/module-common.lds --build-id  -o /home/caozilong/WorkSpace/helloworld/helloworld.ko /home/caozilong/WorkSpace/helloworld/helloworld.o /home/caozilong/WorkSpace/helloworld/helloworld.mod.o**
make[1]:正
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
生成不同版本的 Linux 内核通用的 .ko 模块,可以按照以下步骤进行: 1. 安装内核源代码和编译工具链 在 Linux 系统中,可以通过包管理器安装内核源代码和编译工具链。例如,在 Ubuntu 系统中,可以使用以下命令来安装: ``` sudo apt-get install linux-source build-essential ``` 2. 解压内核源代码 在安装完内核源代码后,需要解压到本地,解压后会生成一个源代码目录。可以使用以下命令来解压: ``` tar xvf linux-source-版本号.tar.xz ``` 3. 进入内核源代码目录,并编译模块 进入解压后的内核源代码目录,使用以下命令进行编译: ``` make modules ``` 编译完成后,会在内核源代码目录的 /lib/modules/版本号 目录下生成 .ko 模块文件。 4. 使用生成的 .ko 模块 生成的 .ko 模块可以在不同版本的 Linux 内核中使用。如果要使用该模块,需要将该模块文件放到系统的 /lib/modules/版本号/kernel/drivers 目录下,并执行以下命令更新模块依赖关系: ``` depmod -a ``` 然后,可以通过 modprobe 命令加载该模块: ``` modprobe 模块名 ``` 加载成功后,可以通过 lsmod 命令查看已加载的模块信息: ``` lsmod ``` 如果要卸载该模块,可以使用以下命令: ``` rmmod 模块名 ``` 卸载成功后,可以通过 lsmod 命令查看已加载的模块信息,确认该模块已被卸载。 总之,要生成通用的 .ko 模块,需要在相同的内核版本下进行编译,然后将生成模块文件放到不同版本的内核中使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值