当前位置: 技术问答>linux和unix
在线等!!!重谢!
来源: 互联网 发布时间:2015-03-10
本文导语: 各位大侠:为什么在对内核进行操作时用printk函数而不是printf呢,而且此程序不能运行. 我的一段程序为: #include /* We're doing kernel work */ #include /* Specifically, a module */ /* Deal with CONFIG_MODVERSIONS */ #i...
各位大侠:为什么在对内核进行操作时用printk函数而不是printf呢,而且此程序不能运行.
我的一段程序为:
#include /* We're doing kernel work */
#include /* Specifically, a module */
/* Deal with CONFIG_MODVERSIONS */
#if CONFIG_MODVERSIONS==1
#define MODVERSIONS
#include
#endif
/* Initialize the module */
int init_module()
{
printk("Hello, world - this is the kernel speakingn");
/* If we return a non zero value, it means that
* init_module failed and the kernel module
* can't be loaded */
return 0;
}
/* Cleanup - undid whatever init_module did */
void cleanup_module()
{
printk("Short is the life of a kernel modulen");
}
我的一段程序为:
#include /* We're doing kernel work */
#include /* Specifically, a module */
/* Deal with CONFIG_MODVERSIONS */
#if CONFIG_MODVERSIONS==1
#define MODVERSIONS
#include
#endif
/* Initialize the module */
int init_module()
{
printk("Hello, world - this is the kernel speakingn");
/* If we return a non zero value, it means that
* init_module failed and the kernel module
* can't be loaded */
return 0;
}
/* Cleanup - undid whatever init_module did */
void cleanup_module()
{
printk("Short is the life of a kernel modulen");
}
|
printf is in the user mode
printk is in the kernel mode
And you ask why ?
I don't know I only know how to use it.
your code no problem.
gcc -c hello.c -I/usr/src/linux-2.4/include -DMODULE -D__KERNEL__
printk is in the kernel mode
And you ask why ?
I don't know I only know how to use it.
your code no problem.
gcc -c hello.c -I/usr/src/linux-2.4/include -DMODULE -D__KERNEL__
|
如果你的机器中内核源码的链接文件为linux-2.4,则试试
gcc -O2 -DMODULE -D__KERNEL__ -I/usr/src/linux-2.4/include -c hello.c
如果为linux,则试试
gcc -O2 -DMODULE -D__KERNEL__ -I/usr/src/linux/include -c hello.c
gcc -O2 -DMODULE -D__KERNEL__ -I/usr/src/linux-2.4/include -c hello.c
如果为linux,则试试
gcc -O2 -DMODULE -D__KERNEL__ -I/usr/src/linux/include -c hello.c
|
这个程序不是直接运行的,是模块
加载用insmod hello.o
卸载用rmmod hello
如果加载不上,就在init函数里面加上一句MODULE_LICENSE("GPL");
加载用insmod hello.o
卸载用rmmod hello
如果加载不上,就在init函数里面加上一句MODULE_LICENSE("GPL");