c - GCC 内核模块编译错误

标签 c linux-kernel network-programming netfilter

我正在研究 netfilter 并在虚拟机上编译它。

matt@ubuntu:~$ make
gcc -c -O2 -W -isystem /lib/modules/4.4.0-87-generic/build/include -D__KERNEL__ -DMODULE test10.c -I. 
In file included from /usr/src/linux-headers-4.4.0-87/include/linux/kernel.h:6:0,
                 from structs1.h:2,
                 from test10.c:1:
/usr/src/linux-headers-4.4.0-87/include/linux/linkage.h:7:25: fatal error: asm/linkage.h: No such file or directory
compilation terminated.
makefile:2: recipe for target 'test' failed
make: *** [test] Error 1

以上是我的 GCC 命令,用于尝试构建我的内核模块以及它抛出的后续错误。

在对此进行研究时,我发现了一种可能的解决方案,它涉及这样指定内核版本:

KERNEL_VER=/usr/src/linux-headers-4.4.0-87/arch/x86/

但是有两个问题:

  1. 除了创建符号链接(symbolic link)之外,我不确定如何在我的 make 文件中实际使用它,如下所示,以及

  2. 我查看了这个文件夹 (/usr/src/linux-headers-4.4.0-87/arch/x86/) 和子文件夹,它没有任何相同的 kernel.h 文件——这正是我需要的。

生成文件:

test:
        gcc -c -O2 -W -isystem /lib/modules/4.4.0-87-generic/build/include -D__KERNEL__ -DMODULE test10.c -I.

如有任何帮助,我们将不胜感激。

最佳答案

用于构建可加载内核模块的标准 Makefile 如下。

obj-m += test10.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

引用Compiling a loadable kernel module了解更多信息。

关于c - GCC 内核模块编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48812053/

相关文章:

c - 什么是具有副作用的表达式?为什么不应将它们传递给宏?

c - 从 sysfs 恢复或删除 Linux 内核模块

c# - NetworkStream.Write 立即返回 - 我如何知道它何时完成发送数据?

networking - 我在哪里可以找到 bittorrent dht bootstrap 节点的列表?

C - dir 路径最后一个文件夹 - 最后是乱码

c - 在从文件中提取的字符串中查找输入子字符串

c - Linux 内核 RSA 签名验证 crypto_akcipher_verify() 输出

ios - 网络故障后恢复时 Swift 中的通知

c - 使用 g++ 编译时未定义对 sem_wait 和 pthread_create 的引用

linux - 有没有办法将 linux 中线程的 cpu 亲和性设置为 "follow"主线程?`