Linux 模块编译:未找到 asm/linkage.h 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9492559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:54:01  来源:igfitidea点击:

module compiling : asm/linkage.h file not found

linuxlinux-kernelkernelkernel-module

提问by Raiden Awkward

I am trying to compile an example of "hello world" Kernel Module, problems found on ubuntu 11.04, kernel 3.2.6, gcc 4.5.2 and fedora 16, kernel 3.2.7, gcc 4.6.7.

我正在尝试编译“hello world”内核模块的示例,在 ubuntu 11.04、内核 3.2.6、gcc 4.5.2 和 fedora 16、内核 3.2.7、gcc 4.6.7 上发现问题。

code:

代码:

#include <linux/module.h>
#include <linux/init.h>
MODULE_LICENSE("GPL");

static int __init hello_init (void)
{
printk("Hello module init\n");
return 0;
}
static void __exit hello_exit (void)
{
printk("Hello module exit\n");
}
module_init(hello_init);
module_exit(hello_exit);

compiled with:

编译:

gcc -D__KERNEL__ -I /usr/src/linux/include/ -DMODULE -Wall -O2 -c hello.c -o hello.o

error:

错误:

In file included from /usr/src/linux/include/linux/kernel.h:13:0, from /usr/src/linux/include/linux/cache.h:4, from /usr/src/linux/include/linux/time.h:7, from /usr/src/linux/include/linux/stat.h:60, from /usr/src/linux/include/linux/module.h:10, from hello.c:1: /usr/src/linux/include/linux/linkage.h:5:25: fatal error: asm/linkage.h: file not found

在 /usr/src/linux/include/linux/kernel.h:13:0、/usr/src/linux/include/linux/cache.h:4、/usr/src/linux/include 包含的文件中/linux/time.h:7,来自 /usr/src/linux/include/linux/stat.h:60,来自 /usr/src/linux/include/linux/module.h:10,来自 hello.c: 1: /usr/src/linux/include/linux/linkage.h:5:25: 致命错误: asm/linkage.h: 找不到文件

then I found in /usr/src/linux/include/ there is no folder named 'asm' but 'asm-generic'; so I made a soft link 'asm' to 'asm-generic', and compiled agail:

然后我发现在 /usr/src/linux/include/ 中没有名为“asm”的文件夹,而是“asm-generic”;所以我做了一个“asm”到“asm-generic”的软链接,然后编译:

this time the error was:

这次的错误是:

In file included from /usr/src/linux/include/linux/preempt.h:9:0, from /usr/src/linux/include/linux/spinlock.h:50, from /usr/src/linux/include/linux/seqlock.h:29, from /usr/src/linux/include/linux/time.h:8, from /usr/src/linux/include/linux/stat.h:60, from /usr/src/linux/include/linux/module.h:10, from hello.c:1: /usr/src/linux/include/linux/thread_info.h:53:29: fatal error: asm/thread_info.h: file not found

在 /usr/src/linux/include/linux/preempt.h:9:0、/usr/src/linux/include/linux/spinlock.h:50、/usr/src/linux/include 包含的文件中/linux/seqlock.h:29,来自 /usr/src/linux/include/linux/time.h:8,来自 /usr/src/linux/include/linux/stat.h:60,来自 /usr/src /linux/include/linux/module.h:10, from hello.c:1: /usr/src/linux/include/linux/thread_info.h:53:29: 致命错误: asm/thread_info.h: file not成立

So I realized I was wrong, but why ? T_T

所以我意识到我错了,但为什么呢?T_T

采纳答案by 2r2w

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

is a proper way to build modules see kbuild documentation

是构建模块的正确方法,请参阅 kbuild 文档

And to see difference beetween your compiler invocation you could

要查看编​​译器调用之间的差异,您可以

cat /lib/modules/$(shell uname -r)/build/Makefile

And analyze an output

并分析输出

回答by ugoren

asmshould be a link to the actual architecture you're compiling for, not to asm-generic.
You can't compile a generic kernel module, that would work on a generic architecture. You have to compile it for the particular architecture you're going to use.

asm应该是指向您正在编译的实际架构的链接,而不是asm-generic.
你不能编译一个通用内核模块,它可以在通用架构上工作。您必须针对将要使用的特定架构编译它。

I don't know why the asmdidn't exist. It should be created as part of the configuration process.
You might get other errors later, if configuration is incomplete in other ways.

我不知道为什么asm不存在。它应该作为配置过程的一部分创建。
如果其他方面的配置不完整,您稍后可能会遇到其他错误。

回答by Barun Parichha

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

Here hello.c is your kernel source file. just use make to build your hello.ko module.

这里 hello.c 是你的内核源文件。只需使用 make 来构建你的 hello.ko 模块。

回答by Abhinav Kumar

module compiling : asm/linkage.h file not found

模块编译:未找到 asm/linkage.h 文件

This means this particular file was not found in specified DIR, which gets specified when we use -I option with make.

这意味着在指定的 DIR 中找不到此特定文件,当我们将 -I 选项与 make 一起使用时会指定该文件。

We can either link that asm-generic to asm, if all headers are present in asm-generic, or we can use make utility.

我们可以将 asm-generic 链接到 asm,如果所有头都存在于 asm-generic 中,或者我们可以使用make 实用程序

Make utility is preferred in case of building kernel modules.

在构建内核模块的情况下,Make 实用程序是首选。

Create a 'Makefile' in working DIR.

在工作目录中创建一个“Makefile”。

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

Use of -C option will change to DIR specified before reading the makefiles or doing anything else.

使用 -C 选项将更改为在读取 makefile 或执行其他任何操作之前指定的 DIR。

So to avoid this error, use -C option with DIR/lib/modules/$(shell uname -r)/build

因此,为避免此错误,请在 DIR 中使用 -C 选项/lib/modules/$(shell uname -r)/build

By this your program will be able to find required files, you will get hello.kofile.

这样您的程序将能够找到所需的文件,您将获得hello.ko文件。

You can add this to kernel modules by

您可以通过以下方式将其添加到内核模块中

sudo insmod hello.ko

Similarly you can remove by

同样,您可以通过删除

sudo rmmod hello

回答by Jay Elston

The asmincludes (such as linkage.h) are architecture specific. There should be a set of directories under:

ASM包括(如linkage.h)是特定结构。下面应该有一组目录:

    /usr/src/kernels/(kernel version goes here)/arch

that provide specific includes for the specific CPU architecture you are targeting your code to be compiled for.

为您要编译的代码的特定 CPU 架构提供特定的包含。

Try adding this to your Makefile:

尝试将其添加到您的 Makefile 中:

    KVERSION :=R(shell uname -r)

and add the kernel and architecture (x86 in this example):

并添加内核和架构(本例中为 x86):

    INCDIRS = -I./include -I/usr/src/kernels/$(KVERSION)/include -I/usr/src/kernels/$(KVERSION)/arch/x86