内核makefile自定义头文件添加 - [linux内核]

本文转载自Wanderer_zjHit的文章,原文链接为:http://www.blogbus.com/wanderer-zjhit-logs/178290410.html

版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
http://www.blogbus.com/wanderer-zjhit-logs/178290410.html

1 参考kbuild Make文档
-- 5.3 External modules using several directories

If an external module does not follow the usual kernel style, but decides to spread files over several directories, then kbuild can
handle this too.
Consider the following example:
|
+- src/complex_main.c
| +- hal/hardwareif.c
| +- hal/include/hardwareif.h
+- include/complex.h
To build a single module named complex.ko, we then need the following kbuild file:
Kbuild: obj-m := complex.o
complex-y := src/complex_main.o
complex-y += src/hal/hardwareif.o
EXTRA_CFLAGS := -I$(src)/include
EXTRA_CFLAGS += -I$(src)src/hal/include
kbuild knows how to handle .o files located in another directory - although this is NOT recommended practice. The syntax is to specify
the directory relative to the directory where the Kbuild file is located.
To find the .h files, we have to explicitly tell kbuild where to look for the .h files. When kbuild executes, the current directory is always the root of the kernel tree (argument to -C) and therefore we have to tell kbuild how to find the .h files using absolute paths.
$(src) will specify the absolute path to the directory where the
Kbuild file are located when being build as an external module. Therefore -I$(src)/ is used to point out the directory of the Kbuild file and any additional path are just appended.
2 example
directory:
--test.h
--test
     --test.c
     --Makefile
2.1 test.h content:
struct test
{        int a;
          int b;
};
2.2.1 test.c content:

#include <linux/init.h>
#include <linux/module.h>
#include <self.h> /*self defination*/

struct test val =  {.a=10,.b=20,};

static int __init module_in(void)
{
        printk("<6> a:%d,b:%d\n",val.a,val.b);
        return 0;
}
static void __exit module_out(void)
{
        ;
}
module_init(module_in);
module_exit(module_out);
2.2.2 Makefile content:

ifneq ($(KERNELRELEASE),)
EXTRA_CFLAGS := -I$(src)/../
obj-m += test.o
else
all:
        $(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) 
install:
        insmod test.ko
uninstall: 
        rmmod test.ko
clean:
        rm -f *.mod.o  *.mod.c *.symvers  *.order *.o *.markers *.ko
endif
3 result:
[10658.704889]  a:10,b:20
kbuild makefile文档翻译连接:

http://bbs.tsinghua.edu.cn/pc/pccon.php?id=6063&nid=144938
附录:内核模块递归调用:

1 在memuconfig中设置宏,如果实现自己的模块,可以不用该宏
当使用类似#make menu
config配置内核成功后,会生成 .config文件。
换句话说:make menuconfig 时,Makefile会从~/arch/i386/Kconfig读取Kconfig.然后根据用户的选择。生成.config文件。
例如:在drivers/hid/Kconfig:
config HID
tristate "Generic HID support"
default y
如果用户选中Y,则配置完成后,在顶层目录下的.config中会有如下一项:
CONFIG_HID=y
2 假设CONFIG_HID=m,即编译为内核模块
在~/drivers/Makefile中可以看到:

obj-$(CONFIG_HID) += hid/
表明:如果CONFIG_HID是M,则把hid目录中的内容编译成为一个内核模块。
进入到/driver/hid目录,则看到:
hid-objs := hid-core.o hid-input.o
表明这两个.o文件是一定会被编译出的。
obj-$(CONFIG_HID) += hid.o
表明:如果CONFIG_HID是M,则hid.o会被编译出来。最后被做成hid.ko


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值