现在的位置: 首页 > 综合 > 正文

empty variable name

2013年12月04日 ⁄ 综合 ⁄ 共 1629字 ⁄ 字号 评论关闭

今天在编写一个最简单的hello驱动,在make的时候出现了“empty variable name”,

最后是如下原因:在Makefile中有如下一句话,出现这个错误的原因是“=”左边多了一个空格

      make -C $(LINUX_SRC) SUBDIRS=$(shell pwd) modules

如果在"="右边多一个空格,则会出现另外的错误:

      *** Error during update of the kernel configuration.

      make[3]: *** [silentoldconfig] Error 1
      make[2]: *** [silentoldconfig] Error 2
      make[1]: Nothing to be done for `/home/chen/hello_module'.
      make[1]: *** No rule to make target `include/config/auto.conf', needed by `include/config/kernel.release'.  Stop.
      make[1]: Leaving directory `/usr/src/linux-headers-2.6.35-22-generic'
      make: *** [all] Error 2
最后的源码hello.c源码:

/*hello_module.c*/

#include "linux/module.h"

#include "linux/init.h"

MODULE_LICENSE("Dual BSD/GPL");
static int __init hello_init(void)
{
    printk(KERN_ALERT "Hello, world\n");
    return 0;
}
static void __exit hello_exit(void)
{
    printk(KERN_ALERT "Goodbye, cruel world\n");
}
module_init(hello_init);
module_exit(hello_exit);

关于hello.c的一点解释:

module_init(hello_init)指定在初始化的时候执行的函数,

module_exit(hello_exit)指定在卸载驱动的时候执行的函数;


另外这里用的是printk问不是printf,这时因为Linux内核不需要标准C库和其他函数库的支持,printk是内核的打印函数;

KERN_ALERT表示打印字符串的优先等级,该等级表示需要立即执行的情况;


Makefile的源码:

LINUX_SRC =/lib/modules/$(shell uname -r)/build
obj-m := hello.o

all:
    make -C $(LINUX_SRC) SUBDIRS=$(shell pwd) modules
    
clean:
    rm -rf *.cmd *.d *.ko *.mod.c *.symvers *.order *.o

效果:

在make生成hello.ko之后执行insmod
./helloworld.ko

这时看不到任何输出,如果再运行 insmod hello.ko还会出现

insmod: error inserting './helloworld.ko': -1 File exists 

这时因为你重复载入模块

用命令 lsmod 可以看到系统里已经有哦这个模块了

重新开一个终端(称为B,原来的终端称为A)

在终端B中,用命令执行cat /proc/kmsg

然后再回到终端A中执行 insmod hello.ko和rmmod hello.ko,便可以在终端B上看到输出(如果提示上述红色标志的错误,请先执行rmmod hello.ko),

也可以在终端B中执行下面两个命令,这时可以看到以前的输出内容

tail /var/log/syslog或cat /var/log/syslog

如果系统一直打印,可以按Ctrl-C手动终止

抱歉!评论已关闭.