Linux .ko模块的生成方式

#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]:正在离开目录 `/usr/src/linux-headers-3.19.0-25-generic'

猜你喜欢

转载自blog.csdn.net/tugouxp/article/details/69053633
今日推荐