关于在Ubuntu的环境下,编译内核模块Hello World!问题!!

内核编译和嵌入式产品的设计与开发
回复
chengfangquan
帖子: 7
注册时间: 2008-04-26 14:34

关于在Ubuntu的环境下,编译内核模块Hello World!问题!!

#1

帖子 chengfangquan » 2008-04-26 22:13

#define MODULE
#include <linux/module.h>

int init_module()
{
printk("<1>Hello World!\n0");
return 0;
}

void cleanup_module()
{
printk("<1> Goodbye!\n");
}



fangquan@fangquan-desktop:~/Desktop/modul$ gcc -c helloworld.c
helloworld.c:2:26: 错误: linux/module.h:No such file or directory
fangquan@fangquan-desktop:~/Desktop/modul$


请问这问题怎么解决,谢谢
东方之子
帖子: 17
注册时间: 2006-11-02 12:12

#2

帖子 东方之子 » 2008-04-27 10:13

先看论坛,再提问
东方之子
帖子: 17
注册时间: 2006-11-02 12:12

#3

帖子 东方之子 » 2008-04-27 10:13

chengfangquan
帖子: 7
注册时间: 2008-04-26 14:34

#4

帖子 chengfangquan » 2008-04-27 22:10

我看过了,下面就是按照他的方法来编译的,出现了一下情况:
fangquan@fangquan-desktop:~/driver$ make
make -C /lib/modules/2.6.20.3-ubuntu1/build M=/home/fangquan/driver modules
make[1]: Entering directory `/usr/src/linux-source-2.6.20'
scripts/Makefile.build:17: /home/fangquan/driver/Makefile: No such file or directory
make[2]: *** 没有规则可以创建目标“/home/fangquan/driver/Makefile”。 停止。
make[1]: *** [_module_/home/fangquan/driver] 错误 2
make[1]: Leaving directory `/usr/src/linux-source-2.6.20'
make: *** [modules] 错误 2
fangquan@fangquan-desktop:~/driver$ ls
hello.c hello.c~ makefile makefile~
fangquan@fangquan-desktop:~/driver$

我想问一下,这是什么原因造成的。
chengfangquan
帖子: 7
注册时间: 2008-04-26 14:34

#5

帖子 chengfangquan » 2008-04-27 22:12

在lib下有以下文件
fangquan@fangquan-desktop:/lib/modules$ ls
2.6.20-15-generic 2.6.20-16-generic 2.6.20.3-ubuntu1
fangquan@fangquan-desktop:/lib/modules$ cd 2.6.20.3-ubuntu1
fangquan@fangquan-desktop:/lib/modules/2.6.20.3-ubuntu1$ ls
build modules.dep modules.ofmap modules.usbmap
kernel modules.ieee1394map modules.pcimap source
modules.alias modules.inputmap modules.seriomap
modules.ccwmap modules.isapnpmap modules.symbols
fangquan@fangquan-desktop:/lib/modules/2.6.20.3-ubuntu1$
chengfangquan
帖子: 7
注册时间: 2008-04-26 14:34

#6

帖子 chengfangquan » 2008-04-27 22:14

在lib下有以下文件
fangquan@fangquan-desktop:/lib/modules$ ls
2.6.20-15-generic 2.6.20-16-generic 2.6.20.3-ubuntu1
fangquan@fangquan-desktop:/lib/modules$ cd 2.6.20.3-ubuntu1
fangquan@fangquan-desktop:/lib/modules/2.6.20.3-ubuntu1$ ls
build modules.dep modules.ofmap modules.usbmap
kernel modules.ieee1394map modules.pcimap source
modules.alias modules.inputmap modules.seriomap
modules.ccwmap modules.isapnpmap modules.symbols
fangquan@fangquan-desktop:/lib/modules/2.6.20.3-ubuntu1$

不知道是否因缺乏安装包造成的
chengfangquan
帖子: 7
注册时间: 2008-04-26 14:34

#7

帖子 chengfangquan » 2008-04-27 22:16

其中 2.6.20.3-ubuntu1 编译内核后生成的
satlantis
帖子: 7
注册时间: 2006-09-25 18:51

#8

帖子 satlantis » 2008-05-01 17:34

是不是你的Makefile有问题呢?
cdefg0912
帖子: 4
注册时间: 2008-07-01 15:30

#9

帖子 cdefg0912 » 2008-07-01 18:33

我也遇到這個問題,可能是因為2.4和2.6的kernel module編譯方式不一樣
你可以試試看
//hello.c
#include<linux/init.h>
#include<linux/module.h>
#include<linux/moduleparam.h>
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void)
{
printk(KERN_ALERT "hello,world\n");
return 0;
}
static int hello_exit(void)
{
printk(KERN_ALERT "goodbye,world\n");
return 0;
}
module_init(hello_init);
module_exit(hello_exit);


//Makefile
ifeq ($(KERNELRELEASE),)
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD :=$(shell pwd)
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
.PHONY:modules modules_install clean
else
obj-m:=hello.o
endif
cdefg0912
帖子: 4
注册时间: 2008-07-01 15:30

#10

帖子 cdefg0912 » 2008-07-01 18:35

$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
記得這裡開頭要用TAB往後退
cdefg0912
帖子: 4
注册时间: 2008-07-01 15:30

#11

帖子 cdefg0912 » 2008-07-01 18:41

參考這裡
http://tw.myblog.yahoo.com/a2267/articl ... 6&next=266
你的code好像是2.4的
cdefg0912
帖子: 4
注册时间: 2008-07-01 15:30

#12

帖子 cdefg0912 » 2008-07-01 19:20

你只要加上Makefile就可以
obj-m:=hello.o
把hello.o改成你的helloworld.o
如後不會顯示出來
請在你可以cat /var/log/syslog |grep hello
查看日志,如果有hello,world就说明成功了。
viewtopic.php?t=126764
spadgeletov
帖子: 92
注册时间: 2008-04-01 0:10

Re: 关于在Ubuntu的环境下,编译内核模块Hello World!问题!!

#13

帖子 spadgeletov » 2008-09-26 2:33

gcc -o helloworld.c 吧
19881004
帖子: 1
注册时间: 2011-08-10 14:00

Re: 关于在Ubuntu的环境下,编译内核模块Hello World!问题!!

#14

帖子 19881004 » 2011-08-10 14:30

我写的源文件hello.c
#include"include/linux/module.h"
#include"include/linux/configfs.h"
#include"linux/init.h"
#include"include/linux/kernel.h"
static int hello_init(void)
{
printk(KERN_ALERT "Hello.world\n ");
return 0;
}
static int hello_exit(void)
{
pritk(KERN_ALERT "Goodbye.crrunt world\n");
return 0;
}
module_init(hello_init);
module_exit(hello_exit);
写的Makefile是:
obj -m +=hello.o
编译用的命令是:make -C '/usr/src/linux-headers-2.6.31-14-generic/' M=$PWD modules
多出来了这么几个文件:
Module.markers modules modules.order Module.symver
编译提示是:
make: Entering directory `/usr/src/linux-headers-2.6.31-14-generic'
Building modules, stage 2.
MODPOST 0 modules
make: Leaving directory `/usr/src/linux-headers-2.6.31-14-generic'
就是没有hello.ko
我是个新手,请问各位前辈,这该怎么解决???
回复