当前位置: 技术问答>linux和unix
最简单的内核编程问题
来源: 互联网 发布时间:2015-12-26
本文导语: hello.c #include #include int init_module() { printk("Hello, world - this is the kernel speakingn"); return 0; } void cleanup_module() { printk("Short is the life of a kernel modulen"); } makefile CC=gcc MODCFLAGS := -O6 -Wall -DCONFIG_KERNELD...
hello.c
#include
#include
int init_module()
{
printk("Hello, world - this is the kernel speakingn");
return 0;
}
void cleanup_module()
{
printk("Short is the life of a kernel modulen");
}
makefile
CC=gcc
MODCFLAGS := -O6 -Wall -DCONFIG_KERNELD -DMODULE -D__KERNEL__ -DLinux
hello.o: hello.c /usr/include/linux/version.h
$(CC) $(MODCFLAGS) -c hello.c
结果一make就出错了
[root@snail]# make
gcc -O6 -Wall -DCONFIG_KERNELD -DMODULE -D__KERNEL__ -DLinux -c hello.c
In file included from /usr/include/linux/module.h:10,
from hello.c:2:
/usr/include/linux/config.h:5:2: error: #error Incorrectly using glibc headers
for a kernel module
hello.c: In function 'init_module':
hello.c:6: warning: implicit declaration of function 'printk'
make: *** [hello.o] Error 1
怎么回事呢?
#include
#include
int init_module()
{
printk("Hello, world - this is the kernel speakingn");
return 0;
}
void cleanup_module()
{
printk("Short is the life of a kernel modulen");
}
makefile
CC=gcc
MODCFLAGS := -O6 -Wall -DCONFIG_KERNELD -DMODULE -D__KERNEL__ -DLinux
hello.o: hello.c /usr/include/linux/version.h
$(CC) $(MODCFLAGS) -c hello.c
结果一make就出错了
[root@snail]# make
gcc -O6 -Wall -DCONFIG_KERNELD -DMODULE -D__KERNEL__ -DLinux -c hello.c
In file included from /usr/include/linux/module.h:10,
from hello.c:2:
/usr/include/linux/config.h:5:2: error: #error Incorrectly using glibc headers
for a kernel module
hello.c: In function 'init_module':
hello.c:6: warning: implicit declaration of function 'printk'
make: *** [hello.o] Error 1
怎么回事呢?
|
CC=gcc
MODCFLAGS := -O2 -Wall -DCONFIG_KERNELD -DMODULE -D__KERNEL__ -DLinux
INCLUDE := /usr/include/linux/
hello.o: hello.c
$(CC) $(MODCFLAGS) -I${INCLUDE} -c hello.c
MODCFLAGS := -O2 -Wall -DCONFIG_KERNELD -DMODULE -D__KERNEL__ -DLinux
INCLUDE := /usr/include/linux/
hello.o: hello.c
$(CC) $(MODCFLAGS) -I${INCLUDE} -c hello.c
|
写MAKEFILE的格式没对,比如:
hello.o : hello.c
gcc .....
注意,上面一行前面是TAB键,而不是用空格,明白?
hello.o : hello.c
gcc .....
注意,上面一行前面是TAB键,而不是用空格,明白?