我正在try 编译一个最小的内核模块,如下所示:

# cat Makefile
obj-m += mymodule.o

all:
        make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
        make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

在执行此操作时,我遇到错误,指出找不到头文件.所以我创建了一个符号链接,它解决了这个问题.但后来我遇到了另一个头文件的相同问题,事情是我无法创建1个指向2个不同位置的SIMLINK.我的理解是不是不正确,或者你怎么解决这个问题?

root@system-yocto:~/kernelModule# make -j
make -C /lib/modules/5.15.54-yocto-standard/build M=/home/root/kernelModule modules
make[1]: Entering directory '/lib/modules/5.15.54-yocto-standard/build'
warning: the compiler differs from the one used to build the kernel
  The kernel was built by: arm-poky-linux-gnueabi-gcc (GCC) 11.4.0
  You are using:           gcc (GCC) 11.4.0
  CC [M]  /home/root/kernelModule/mymodule.o
In file included from ./include/linux/init.h:5,
                 from /home/root/kernelModule/mymodule.c:1:
./include/linux/compiler.h:255:10: fatal error: asm/rwonce.h: No such file or directory
  255 | #include <asm/rwonce.h>
      |          ^~~~~~~~~~~~~~
compilation terminated.
make[2]: *** [scripts/Makefile.build:288: /home/root/kernelModule/mymodule.o] Error 1
make[1]: *** [Makefile:1886: /home/root/kernelModule] Error 2
make[1]: Leaving directory '/lib/modules/5.15.54-yocto-standard/build'
make: *** [Makefile:4: all] Error 2
root@system-yocto:~/kernelModule# ls -l /lib/modules/5.15.54-yocto-standard/source/include/
total 204
drwxr-xr-x  3 root root  4096 Mar  9 12:34 acpi
drwxr-xr-x  4 root root  4096 Mar  9 12:42 asm-generic
drwxr-xr-x  2 root root  4096 Mar  9 12:34 clocksource
drwxr-xr-x  2 root root 53248 Mar  9 12:34 config
drwxr-xr-x  3 root root  4096 Mar  9 12:34 crypto
drwxr-xr-x  5 root root  4096 Mar  9 12:34 drm
drwxr-xr-x 37 root root  4096 Mar  9 12:34 dt-bindings
drwxr-xr-x  3 root root  4096 Mar  9 12:34 generated
drwxr-xr-x  2 root root  4096 Mar  9 12:34 keys
drwxr-xr-x  2 root root  4096 Mar  9 12:34 kunit
drwxr-xr-x  2 root root  4096 Mar  9 12:34 kvm
drwxr-xr-x 65 root root 40960 Mar  9 12:34 linux
drwxr-xr-x  2 root root  4096 Mar  9 12:34 math-emu
drwxr-xr-x  6 root root  4096 Mar  9 12:34 media
drwxr-xr-x  2 root root  4096 Mar  9 12:34 memory
drwxr-xr-x  2 root root  4096 Mar  9 12:34 misc
drwxr-xr-x 12 root root  4096 Mar  9 12:34 net
drwxr-xr-x  2 root root  4096 Mar  9 12:34 pcmcia
drwxr-xr-x  2 root root  4096 Mar  9 12:34 ras
drwxr-xr-x  2 root root  4096 Mar  9 12:34 rdma
drwxr-xr-x  3 root root  4096 Mar  9 12:34 scsi
drwxr-xr-x 16 root root  4096 Mar  9 12:34 soc
drwxr-xr-x  4 root root  4096 Mar  9 12:34 sound
drwxr-xr-x  3 root root  4096 Mar  9 12:34 target
drwxr-xr-x  3 root root  4096 Mar  9 12:34 trace
drwxr-xr-x 12 root root  4096 Mar  9 12:34 uapi
drwxr-xr-x  2 root root  4096 Mar  9 12:34 vdso
drwxr-xr-x  2 root root  4096 Mar  9 12:34 video
drwxr-xr-x  4 root root  4096 Mar  9 12:34 xen
root@system-yocto:~/kernelModule# ln -s /lib/modules/5.15.54-yocto-standard/source/include/asm-generic/ /lib/modules/5.15.54-yocto-standard/source/include/asm
root@system-yocto:~/kernelModule# make -j
make -C /lib/modules/5.15.54-yocto-standard/build M=/home/root/kernelModule modules
make[1]: Entering directory '/lib/modules/5.15.54-yocto-standard/build'
warning: the compiler differs from the one used to build the kernel
  The kernel was built by: arm-poky-linux-gnueabi-gcc (GCC) 11.4.0
  You are using:           gcc (GCC) 11.4.0
  CC [M]  /home/root/kernelModule/mymodule.o
In file included from ./include/linux/errno.h:5,
                 from ./include/linux/string.h:8,
                 from ./include/linux/bitmap.h:10,
                 from ./include/linux/cpumask.h:12,
                 from ./include/linux/mm_types_task.h:14,
                 from ./include/linux/mm_types.h:5,
                 from ./include/linux/buildid.h:5,
                 from ./include/linux/module.h:14,
                 from /home/root/kernelModule/mymodule.c:2:
./include/uapi/linux/errno.h:1:10: fatal error: asm/errno.h: No such file or directory
    1 | #include <asm/errno.h>
      |          ^~~~~~~~~~~~~
compilation terminated.
make[2]: *** [scripts/Makefile.build:288: /home/root/kernelModule/mymodule.o] Error 1
make[1]: *** [Makefile:1886: /home/root/kernelModule] Error 2
make[1]: Leaving directory '/lib/modules/5.15.54-yocto-standard/build'
make: *** [Makefile:4: all] Error 2

推荐答案

我觉得你应该试一下/lib/modules/yourKernelVersion里面的$make prepare. 不太清楚为什么,但在过go 的不同Make操作中遇到了类似的问题,为我解决了这个问题.

C++相关问答推荐

当包含头文件时,gcc会发出隐式函数声明警告

有效地计算由一组点构成的等边三角形和等腰三角形的数量

ATTiny1606定时器TCA 0中断未触发

如何创建由符号组成的垂直结果图形?

__VA_OPT__(,)是否可以检测后面没有任何内容的尾随逗号?

减法运算结果的平方的最快方法?

在C++中头文件中声明外部 struct

实现简单字典时C语言中的段错误

如何用c语言修改shadow文件hash部分(编程)?

如何使用_newindex数组我总是得到错误的参数

在txt文件中找到指定的字符串,并从数字中减go 相同的值

预处理器宏扩展(ISO/IEC 9899:1999(E)§;6.10.3.5示例3)

如何在c中使用具有不同变量类型的内存分配?

浮点正零何时不全为零?

按字典顺序打印具有给定字符的所有可能字符串

C: NULL>;NULL总是false?

如何找出C中分配在堆上的数组的大小?

使用 _Atomic float 时,MSVC 编译的代码会命中调试断言

与 C 相比,C++ 中无副作用的无限循环的好处是 UB?

将十六进制值或十进制值分配给 uint16_t 有什么区别?