Chinaunix

标题: Makefile 内核模块 包含非内核头文件该如何修改? [打印本页]

作者: linhos    时间: 2017-04-06 16:54
标题: Makefile 内核模块 包含非内核头文件该如何修改?
小弟最近根据论坛里的方法编写了一个基于Netfilter 的hook 模块(checkIP.c)。
用的方法基本上是http://bbs.chinaunix.net/thread-1941060-1-1.html 白手起家 大神里面的方法。
但是我的方法略有不同,使用了linux 自带的 正则表达式方法(regex.h 文件在/usr/include 文件夹里面)
本人根据网上的各种说明写了一个Makefile 但是不能正常编译,
Makefile 编写如下:
obj-m :=checkIP.o
KERNELDIR := /lib/modules/$(shell uname -r)/build
PWD :=$(shell pwd)
modules:
        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
        rm -rf *.o *~core .*.cmd *.mod.c ./tmp version


每次编译都会出现问题,不能正常编译。
问题如下:
make -C /lib/modules/4.4.0-67-generic/build M=/home/linhos/progect/c++/untitled/modul modules
make[1]: Entering directory '/usr/src/linux-headers-4.4.0-67-generic'
  CC [M]  /home/linhos/progect/c++/untitled/modul/checkIP.o
/home/linhos/progect/c++/untitled/modul/checkIP.c:16:19: fatal error: regex.h: 没有那个文件或目录
compilation terminated.
scripts/Makefile.build:264: recipe for target '/home/linhos/progect/c++/untitled/modul/checkIP.o' failed
make[2]: *** [/home/linhos/progect/c++/untitled/modul/checkIP.o] Error 1
Makefile:1420: recipe for target '_module_/home/linhos/progect/c++/untitled/modul' failed
make[1]: *** [_module_/home/linhos/progect/c++/untitled/modul] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-4.4.0-67-generic'
Makefile:5: recipe for target 'modules' failed
make: *** [modules] Error 2



跪求各位大神,我的Makefile该怎么写?


作者: nswcfd    时间: 2017-04-06 18:00
kernel的好像没有regex.h(不知道最近有了没有)

kernel里所有的#include,都是基于楼主Makefile的$KERNELDIR目录,一般不能跳出这个范围之内。
作者: jeppeter    时间: 2017-04-07 09:43
本帖最后由 jeppeter 于 2017-04-07 09:50 编辑

回复 1# linhos

内核是不能直接用库函数的头文件的,这一点一定要记住,因为库文件本身是依赖内核的,但内核不依赖库文件。如果你要在内核中引入REGEX,那得先GOOGLE一下,这个是https://github.com/xnsystems/kpcre
如果你要引用另外的目录,内核中编译中有一个专门的
  1. EXTRA_CFLAGS=-I$(PWD)/inc
复制代码

放在你的MAKEFILE头上就可以了。





欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2