C语言 使用较新的编译器编译 linux 2.6 内核模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29925513/
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-09-02 11:54:01  来源:igfitidea点击:

Compile a linux 2.6 kernel module with newer compiler

cgcclinux-kernel

提问by user761576

I build embedded machines that run an RT_PREMPT version of Linux. It's an Ubuntu 10.04 installation running an Linux 2.6 kernel. Yes, it's an old kernel, but I'm stuck with it for awhile.

我构建了运行 RT_PREMPT 版本的 Linux 的嵌入式机器。这是一个运行 Linux 2.6 内核的 Ubuntu 10.04 安装。是的,这是一个旧内核,但我坚持了一段时间。

When I compiled the kernel, I used gcc version 4.4. On this system, there is a kernel module I have been compiling successfully for three years.

当我编译内核时,我使用了 gcc 4.4 版。在这个系统上,有一个内核模块我已经编译成功了三年。

From my Makefile...

从我的 Makefile...

all:

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

My current project requires support for c++14, so I updated gcc and g++ to version 5.1.0 by building from source. All my user-mode software compiles, but when I went to build an updated version of my kernel module, I get the following error right away:

我当前的项目需要支持 c++14,所以我通过从源代码构建将 gcc 和 g++ 更新到版本 5.1.0。我所有的用户模式软件都可以编译,但是当我去构建内核模块的更新版本时,我立即收到以下错误:

make[1]: Entering directory `/usr/src/linux-headers-2.6.32-54-generic'
  CC [M]  /home/tbj/srcroot/ctsengine-hg/CtsRt/ctsrt_main.o
In file included from include/linux/compiler.h:40:0,
             from include/linux/stddef.h:4,
             from include/linux/list.h:4,
             from include/linux/module.h:9,
             from /home/tbj/srcroot/ctsengine-hg/CtsRt/ctsrt_main.c:31:
include/linux/compiler-gcc.h:86:30: fatal error: linux/compiler-gcc5.h: No such file or directory

In short:

简而言之:

fatal error: linux/compiler-gcc5.h: No such file or directory

If I use gcc 4.4 again (I left it installed on my computer in a different directory), it compiles and runs perfectly.

如果我再次使用 gcc 4.4(我将它安装在我的计算机上的不同目录中),它会编译并完美运行。

Obviously, I'm missing something. Is it not possible to compile a kernel module with a newer version of the compiler than what the operating system was compiled with? That seems unlikely to me. Is there a configuration step I'm missing? Is there a system variable I need to update? Are there extra headers I'm supposed to download? I ran apt to update build-essential, but it was up to date. I have the source and headers for the kernel on my system. I'm not sure what else I would download.

显然,我错过了一些东西。是否无法使用比操作系统编译的编译器版本更新的编译器来编译内核模块?这对我来说似乎不太可能。是否有我遗漏的配置步骤?是否有我需要更新的系统变量?我应该下载额外的标题吗?我跑去更新 build-essential,但它是最新的。我的系统上有内核的源代码和头文件。我不确定我还会下载什么。

Any insights to this problem are greatly appreciated. Thank you in advance.

非常感谢对此问题的任何见解。先感谢您。

采纳答案by baf

Your kernel version is too old and it is missing linux/compiler-gcc5.h.

您的内核版本太旧,已丢失linux/compiler-gcc5.h

The header is included from linux/compiler-gcc.hand its name is generated by preprocessor macro based on current compiler version:

头文件包含在linux/compiler-gcc.h 中,其名称由预处理器宏根据当前编译器版本生成:

#define _gcc_header(x) __gcc_header(linux/compiler-gcc##x.h)

This header was introduced around version 3.18.

这个头文件是围绕 version 引入的3.18

You might try to get this file from a newer kernel source and put in into include/linux.

您可能会尝试从较新的内核源获取此文件并将其放入include/linux.

回答by Filippo Lauria

Open a terminal and type:

打开终端并输入:

$ ls -r /usr/src/linux-headers-$(uname -r)/include/linux/ | \
  grep -P "compiler-gcc\d.h" | \
  head -n 1

this will hopefully output the latest gcc header suitable for your linux kernel.

这将有望输出适合您的 linux 内核的最新 gcc 头文件。

In my case it is compiler-gcc4.h. Now you can link this file to compiler-gcc5.hin the following way:

就我而言,它是compiler-gcc4.h. 现在您可以compiler-gcc5.h通过以下方式将此文件链接到:

$ sudo ln -s /usr/src/linux-headers-$(uname -r)/include/linux/compiler-gcc4.h \
             /usr/src/linux-headers-$(uname -r)/include/linux/compiler-gcc5.h