使用gcc和内核头文件编译Linux内核模块 [英] Compiling Linux kernel module using gcc with kernel header files

查看:1114
本文介绍了使用gcc和内核头文件编译Linux内核模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一台内核A头文件的Linux机器。
我想用内核A的GCC编译一个C程序,而内核B正在运行。



我该怎么做?如何检查它是否有效?

解决方案

这是需要深入研究的其他信息。 Post 2.6版本,正如其他答复中提到的那样,Makefile负责处理大部分Linux内核模块编译步骤。然而,它的核心仍然是GCC,这就是它的作用:(你也可以在没有Makefile的情况下编译它)



以下GCC选项是必需的: p>


  1. -isystem / lib / modules /`uname -r` / build / include:你正在编译的内核的内核头文件。使用
    缺省的/ usr / include中/ Linux将无法正常工作


  2. -D__KERNEL __ :定义这个符号告诉代码将以内核模式运行的头文件,而不是作为用户进程运行。

  3. 头文件,得到适当的定义为一个内核模块




    GCC -DMODULE - D__KERNEL__ -isystem / lib / modules / $(uname -r)/ build / include -c hello.c -o hello.ko


    I have a Linux machine with kernel A header files. I want to compile a C program using GCC with kernel A while kernel B is currently running.

    How can I do that? How do I check that it works?

    解决方案

    This is additional info to delve into. Post 2.6 version, as mentioned in other reply the Makefile takes care of most of the Linux kernel module compilation steps. However, at the core of it is still GCC, and this is how it does: (you too may compile it without Makefile)

    Following GCC options are necessary:

    1. -isystem /lib/modules/`uname -r`/build/include: You must use the kernel headers of the kernel you're compiling against. Using the default /usr/include/linux won't work.

    2. -D__KERNEL__: Defining this symbol tells the header files that the code will be run in kernel mode, not as a user process.

    3. -DMODULE: This symbol tells the header files to give the appropriate definitions for a kernel module.

    gcc -DMODULE -D__KERNEL__ -isystem /lib/modules/$(uname -r)/build/include -c hello.c -o hello.ko

    这篇关于使用gcc和内核头文件编译Linux内核模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆