起因
最近想写linux设备驱动,发现不同参考书上的Makefile文件风格不一致,不一致是小事,我自己对这方面知识的欠缺总感觉穿着很重的鞋子走路。当然,网上的资料也是良莠不齐,自己看内核代码Document/kbuild中的文件算是做一下读书笔记,有错误的话还请谅解。
简介
“kbuild” is build system used by the linux kernel.Modules must use kbuild to stay compatible with GCC. Modules programming is consist of in-tree and out-of-tree
系统命令
$make -C
M=$PWD $make -C /lib/modules/\`uname -r\`/build M=$PWD
target:
modules_install
$make -C /lib/modules/`uname -r`/build modules_install
选项
tips: $KDIR is short of path of the kernel source directory
-C $KDIR make命令会自动改变到这个特殊的kernel source目录
M=$PWD 通知kbuild一个外部模块已经被建立,”M”是这个外部模块(kbuild file)所在的绝对路径
target
make -C $KDIR M=$PWD [target]
模块默认在这个文件夹生成(哪个?),所有的文件都会在这个文件夹中
modules:
同上
modules_install:
同上
clean:
仅仅删除在模块目录中生成的所有文件
help:
从来没用过
建立分离文件
最简单的例子:
obj-m := <module_name>.o
这kbuild系统将会根据
如果模块是由多个文件构建而来,则是一下格式
<module_name>-y := <src1>.o <src2>.o
例子
ifneq ($(KERNELRELEASE),)
#kbuid part of Makefile
obj-m := 8123.o
8123-y := 8123_if.o 8123_pci.o 8123_bin.o
else
#normal makefile
KDIR ?=/lib/modules/`uname -r`/build
default:
$(MAKE) -C $(KDIR) M=$$PWD
genbin:
echo "x" > 8123_bin.o_shipped
endif
Makefile样本
经过本人亲自测试,下面的Makefile文件真实有效,其来源与LDD(Linux Decives Drives)
ifneq ($(KERNELRELEASE),)
obj-m := __task_pid_nr_ns.o
else
PWD := $(shell pwd)
KVER := $(shell uname -r)
KDIR := /lib/modules/$(KVER)/build
default:
$(MAKE) -C $(KDIR) M=$(PWD) modules
endif
我们的源文件取名为task_pid_nr_ns.c,一会我会把代码写上来,看来linux编程真的是博大精深啊。
update
==========2017-11-26=uodate=========
First you have downloaded the source code from www.kernel.org with git or other tools.then you can make menuconfigmake -j8make modules_install install and update grub,you reboot your machine.please refer to here
But, if you like source code and output separated:
you would better use:
export KBUILD_OUTPOT=$HOME/src/kernel-build
Even you type ‘make menuconfig && make ‘ in source file, the output will put into KBUILD_OUTPOT, then , you must move into KBUILD_OUTPOT dir type”make modules_install install” command.
But, if i modify some code, how to i did?