简体   繁体   English

在内核中构建hello world模块时出错

[英]Error in building hello world module in kernel

/*
 *  hello-1.c - The simplest kernel module.
 */

#include <linux/module.h>       /* Needed by all modules */
#include <linux/kernel.h>       /* Needed for KERN_INFO */

int init_module(void)
{
        printk(KERN_INFO "Hello world 1.\n");

        /*
         * A non 0 return means init_module failed; module can't be loaded.
         */
        return 0;
}

void cleanup_module(void)
{
        printk(KERN_INFO "Goodbye world 1.\n");
}

MAKEFILE 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

After the make command I am getting the following error. make命令后,我收到以下错误。 What is the reason for this error and how can i resolve this? 这个错误的原因是什么?我该如何解决这个问题?

make -C /lib/modules/2.6.32-358.el6.x86_64/build M=/home/hello modules
make: *** /lib/modules/2.6.32-358.el6.x86_64/build: No such file or directory.  Stop.
make: *** [all] Error 2

-C option in make command instruct to change directory to one provided with -C. make命令中的-C选项指示将目录更改为-C提供的目录。 In your case, it is /lib/modules/2.6.32-358.el6.x86_64/build. 在您的情况下,它是/lib/modules/2.6.32-358.el6.x86_64/build。 But when it tries to change to that directory, compilation gives error with "No such file or directory", which means that you don't have build directory at /lib/modules/2.6.32-358.el6.x86_64/. 但是当它尝试更改到该目录时,编译会出现“没有这样的文件或目录”的错误,这意味着您没有/lib/modules/2.6.32-358.el6.x86_64/中的构建目录。

A lot of time it happens that build in the given path may not be a directory, but it may be a soft link pointing to a kernel source code directory. 很多时候,在给定路径中构建可能不是目录,但它可能是指向内核源代码目录的软链接。

So you need to check either there should be build directory at the required path which contains kernel source or it should be a soft link to kernel source. 因此,您需要检查在包含内核源的所需路径上是否应该有构建目录,或者它应该是到内核源的软链接。

Apart from all those build related comments, there are few things you have to follow while writing a kernel module. 除了所有那些构建相关的注释之外,在编写内核模块时您必须遵循的一些事项。 Your code will not work as you expect even after you fix those build issues. 即使您修复了这些构建问题,您的代码也无法按预期工作。 Why? 为什么? you have the init and cleanup routines built, but you haven't specified/directed which one is your init routine and which is cleanup. 你已经构建了init和cleanup例程,但是你没有指定/指示哪一个是你的init例程,哪个是清理。

You have to do that this way, 你必须这样做,

at the end of the file add these two line. 在文件的末尾添加这两行。

module_init ( foo_init_fn);

module_exit ( bar_exit_fn);

Also, you have to specify the module license details etc. like below 此外,您必须指定模块许可证详细信息等,如下所示

MODULE_LICENSE("GPL");

MODULE_AUTHOR("Copyright (c) 2006 by xxxx xxxxx, Inc.");

MODULE_DESCRIPTION("klm_vdc");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM