日期:2014-05-16  浏览次数:20763 次

linux kernel 测试文件编译失败
学习《Linux设备驱动程序第三版.pdf》,第2章的给的测试程序,无法再虚拟机上编译成功。主要原因是makefile不正确,请帮忙修改一下。
错误提示信息:
make[1]: Entering directory `/usr/src/kernels/2.6.29.4-167.fc11.i686.PAE'
make[2]: *** No rule to make target `/mnt/hgfs/share/kernel_code/kernel_hello.c', needed by `/mnt/hgfs/share/kernel_code/kernel_hello.o'. Stop.
make[1]: *** [_module_/mnt/hgfs/share/kernel_code] Error 2
make[1]: Leaving directory `/usr/src/kernels/2.6.29.4-167.fc11.i686.PAE'
make: *** [default] Error 2

书中的makefile脚本(没有指定编译器,我不清楚应该指定那个编译器)
Python code

# If KERNELRELEASE is defined, we've been invoked from the
# kernel build system and can use its language.
ifneq ($(KERNELRELEASE),)
obj-m := kernel_hello.o
# Otherwise we were called directly from the command
# line; invoke the kernel build system.
else
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
#ehco $(KERNELDIR)
PWD := $(shell pwd)
#echo $(PWD)
default:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules


源代码:
C/C++ code

#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void)
{
printk(KERN_ALERT "Hello, world\n");
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT "Goodbye, cruel world\n");
}
module_init(hello_init);
module_exit(hello_exit);



请问应该如何修改makefile

------解决方案--------------------
对于第一个问题:当你使用伪终端执行的时候是看不到打印的字符的,如果你切换到真正的字符界面(终端)在insmod,就能够看到打印的消息了;
第二个问题没有接触过
探讨
问题已经解决。原因是源码文件用了.cpp的后缀导致编译失败。
但是又遇到新的问题:
1.inmod kernel_hello.ko 和rmmod kernel_hello.ko 并没有打印任何字符串
2.如果源码文件用cpp后缀如何编译呢?