简体   繁体   English

为Linux内核的简单hello world模块生成.ko文件时出错

[英]error in generating .ko file for simple hello world module for linux kernel

I am a beginner in linux kernel development and trying to load a simple module in linux. 我是linux内核开发的初学者,并试图在linux中加载一个简单的模块。 I have created an hello.c file, to be loaded as kernel module. 我创建了一个hello.c文件,将其作为内核模块加载。

 #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); 

this hello.c and the makefile both, I have kept in /home/linux/ directory. 这个hello.c和makefile都保存在/ home / linux /目录中。

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 

to generate .ko file, when I run the make command on terminal from the /home/linux directory , I get following error 生成.ko文件,当我从/ home / linux目录在终端上运行make命令时,出现以下错误

 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 # Module Name is hello.c obj-m:= hello.o#模块名称是hello.c

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

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

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

its not guaranteed that headers file will always be located in /usr/src directory, but it will surely be located in /lib/modules directory. 它不能保证头文件将始终位于/ usr / src目录中,但一定会位于/ lib / modules目录中。

  • make sure that system has latest header files 确保系统具有最新的头文件

to find out which header files to be present run ` 找出要运行的头文件

uname -r 匿名-r

on terminal, output will be like 在终端上,输出将像

3.5.0-17-generic

to install header files run 安装头文件运行

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

You have: 你有:

$(MAKE) -C $(src) SUBDIR-$(PWD) modules

But it seems like you want: 但似乎您想要:

$(MAKE) -C $(src)/SUBDIR-$(PWD) modules

Or something along those lines; 或类似的规定; where does the source code live? 源代码在哪里? You need to -C there. 您需要在那里-C

Kernel build system is a bit complex. 内核构建系统有点复杂。 It would be good to read the kernel build process documentation. 最好阅读内核构建过程文档。 Which gives better understanding about, say, 这样可以更好地理解例如

  • Targets like --- modules / modules_install 目标,如--- modules / modules_install
  • Options like --- -C $KDIR / M=$PWD 像--- -C $KDIR / M=$PWD
  • Command Syntax --- 命令语法-
  • $ make -C <path_to_kernel_src> M=$PWD

    $ make -C /lib/modules/ uname -r /build M=$PWD $ make -C /lib/modules/ uname -r /build M=$PWD

    $ make -C /lib/modules/ uname -r /build M=$PWD modules_install $ make -C /lib/modules/ uname -r /build M=$PWD modules_install

  • Loadable module goals --- obj-m 可加载模块目标--- obj-m

etc... 等等...

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

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