搞定了内核树后,又过了半天才最终搞定helloworld,下面说说整个过程吧!
程序本身用的是ldd3的代码:
/*
*hello.c
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.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的。之前做的都是高级编程,从来就没有写什么makefile文件的习惯。上网检索,介绍的makefile的写法要不就是太难了,要不就是对于我的问题没有针对性,即使看懂了也无組问题的解决。最后在ldd3本章的“编译与加载”部分介绍了一个简单明了的写法,然后照葫芦画瓢才最终解决,最终终于看到这几天努力的成过了!
不过这里特别要注意的是:命令行make -C /usr/src/linux-2.6.23 M=`pwd` modules 中单引号必须是‘ ` ’ ,而不是‘ ' ’,我就是因为这个问题困惑了我很久。
makefile文件内容: obj-m := hello.o
终端输入命令行:make -C /usr/src/linux-2.6.23 M=`pwd` modules
其中-C 后面是我的源码所在的文件夹;
编译时显示的内容:
make: Entering directory `/usr/src/linux-2.6.23'
CC [M] /home/hello.o
Building modules, stage 2.
MODPOST
CC /home/hello.mod.o
LD [M] /home/hello.ko
make: Leaving directory `/usr/src/linux-2.6.23'
这里要注意的两个问题:
Makefile文件的名称是:‘Makefile’,而不是'makefile';
编译的时候输入的文件名称是helloworld.c,而不是helloworld;如果你源程序文件的名称是helloworld,清改为helloworld.c
程序本身用的是ldd3的代码:
/*
*hello.c
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.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的。之前做的都是高级编程,从来就没有写什么makefile文件的习惯。上网检索,介绍的makefile的写法要不就是太难了,要不就是对于我的问题没有针对性,即使看懂了也无組问题的解决。最后在ldd3本章的“编译与加载”部分介绍了一个简单明了的写法,然后照葫芦画瓢才最终解决,最终终于看到这几天努力的成过了!
不过这里特别要注意的是:命令行make -C /usr/src/linux-2.6.23 M=`pwd` modules 中单引号必须是‘ ` ’ ,而不是‘ ' ’,我就是因为这个问题困惑了我很久。
makefile文件内容: obj-m := hello.o
终端输入命令行:make -C /usr/src/linux-2.6.23 M=`pwd` modules
其中-C 后面是我的源码所在的文件夹;
编译时显示的内容:
make: Entering directory `/usr/src/linux-2.6.23'
CC [M] /home/hello.o
Building modules, stage 2.
MODPOST
CC /home/hello.mod.o
LD [M] /home/hello.ko
make: Leaving directory `/usr/src/linux-2.6.23'
这里要注意的两个问题:
Makefile文件的名称是:‘Makefile’,而不是'makefile';
编译的时候输入的文件名称是helloworld.c,而不是helloworld;如果你源程序文件的名称是helloworld,清改为helloworld.c