鍍金池/ 問答/Python  C  Linux/ centos7內(nèi)核模塊編譯報(bào)錯(cuò)

centos7內(nèi)核模塊編譯報(bào)錯(cuò)

問題背景
使用Makefile完成Linux內(nèi)核模塊程序的編譯,出現(xiàn)了一下問題

代碼結(jié)構(gòu)

|-firewall
|------log.h
|------log.c
|------demo.c    // 引入log.h
|------Makefile

Makefile文件

ifneq ($(KERNELRELEASE),)
    obj-m += firewall.o
    firewall-objs := demo.o log.o

else      
    KDIR := /lib/modules/$(shell uname -r)/build
    PWD:= $(shell pwd)

    SUBDIRS := log

all:
    make -C $(KDIR) M=$(PWD) modules

clean:
    rm -f *.ko *.o *.mod.o *.mod.c *.symvers

endif  

報(bào)錯(cuò)

make -C /lib/modules/3.10.0-693.el7.x86_64/build M=/root/work/firewall/source modules
make[1]: 進(jìn)入目錄“/usr/src/kernels/3.10.0-693.el7.x86_64”
  CC [M]  /root/work/firewall/source/demo.o
In file included from /root/work/firewall/source/demo.c:7:0:
/root/work/firewall/source/log.h:4:19: 致命錯(cuò)誤:stdio.h:沒有那個(gè)文件或目錄
 #include <stdio.h>
                   ^
編譯中斷。
make[2]: *** [/root/work/firewall/source/demo.o] 錯(cuò)誤 1
make[1]: *** [_module_/root/work/firewall/source] 錯(cuò)誤 2
make[1]: 離開目錄“/usr/src/kernels/3.10.0-693.el7.x86_64”
make: *** [all] 錯(cuò)誤 2

由上述可知,報(bào)錯(cuò)原因致命錯(cuò)誤:stdio.h:沒有那個(gè)文件或目錄,但是系統(tǒng)是包含stdio.h文件的,位置在

find / -name "stdio.h"
/usr/include/bits/stdio.h
/usr/include/stdio.h

并且執(zhí)行gcc -c test.c是成功的,文件內(nèi)容如下

/*
*@file test.c 測試stdio.h存在于系統(tǒng)
*/
#include <stdio.h>
int main() {
}

問題
綜上所述,根本原因不是缺少stdio.h文件,而是Makefile文件有問題,哪位大神能指點(diǎn)一下?(打字不易,非誠勿擾

回答
編輯回答
孤酒

你不可以在內(nèi)核模塊中引用 stdio.h,因?yàn)樗鼉H用于應(yīng)用程序,它的實(shí)現(xiàn)依賴于

  1. GNU libc, https://www.gnu.org/software/...
  2. 或 musl libc, https://www.musl-libc.org/

等第三方庫。

2017年8月25日 09:58