为Linux内核的简单hello world模块生成.ko文件时出错 [英] error in generating .ko file for simple hello world module for linux kernel

查看:197
本文介绍了为Linux内核的简单hello world模块生成.ko文件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是linux内核开发的初学者,并试图在linux中加载一个简单的模块. 我创建了一个hello.c文件,将其作为内核模块加载.

I am a beginner in linux kernel development and trying to load a simple module in linux. I have created an hello.c file, to be loaded as kernel module.

#include <linux/module.h>  
#include <linux/kernel.h>  
#include <linux/init.h>    

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("A Simple Hello World module");

static int __init hello_init(void)
{
    printk(KERN_INFO "Hello world!\n");
    return 0;   
}

static void __exit hello_cleanup(void)
{
    printk(KERN_INFO "Cleaning up module.\n");
}

module_init(hello_init);
module_exit(hello_cleanup);

这个hello.c和makefile都保存在/home/linux/目录中.

this hello.c and the makefile both, I have kept in /home/linux/ directory.

makefile

obj-m +=hello.o

src= /usr/src/linux-headers-3.5.0-17-generic
all:
  $(MAKE) -C $(src) SUBDIR-$(PWD) modules
clean:
  rm -rf *.o *.ko

生成.ko文件,当我从/home/linux目录在终端上运行make命令时,出现以下错误

to generate .ko file, when I run the make command on terminal from the /home/linux directory , I get following error

h2o@h2o-Vostro-1015:~/linux$ make
make -C /usr/src/linux-headers-3.5.0-17-generic SUBDIR-/home/h2o/linux modules
make[1]: Entering directory `/usr/src/linux-headers-3.5.0-17-generic'
make[1]: *** No rule to make target `SUBDIR-/home/h2o/linux'.  Stop.
make[1]: Leaving directory `/usr/src/linux-headers-3.5.0-17-generic'
make: *** [all] Error 2

请告知我我在想什么或做错了什么.

kindly advise what am I missing or doing wrong..

推荐答案

  • Makefile

    • Makefile

      obj-m:= hello.o#模块名称是hello.c

      obj-m := hello.o # Module Name is hello.c

      KDIR:=/lib/modules/$(shell uname -r)/build

      KDIR := /lib/modules/$(shell uname -r)/build

      全部:$(MAKE)-C $(KDIR)M = $(PWD)个模块

      all: $(MAKE) -C $(KDIR) M=$(PWD) modules

      clean:$(MAKE)-C $(KDIR)M = $(PWD)clean $(RM)Module.markers modules.order

      clean: $(MAKE) -C $(KDIR) M=$(PWD) clean $(RM) Module.markers modules.order

    • 不能保证头文件始终位于/usr/src目录中,但可以肯定地位于/lib/modules目录中.

      its not guaranteed that headers file will always be located in /usr/src directory, but it will surely be located in /lib/modules directory.

      • 确保系统具有最新的头文件

      找出要显示的头文件 运行`

      to find out which header files to be present run `

      uname -r

      uname -r

      在终端上,输出将类似于

      on terminal, output will be like

      3.5.0-17-generic

      运行安装头文件

      sudo apt-get install linux-headers-$(uname -r)

      这篇关于为Linux内核的简单hello world模块生成.ko文件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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