免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2548 | 回复: 6
打印 上一主题 下一主题

LINUX内核编程的入门程序hello world,希望高手来指点一下 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-09-26 09:43 |只看该作者 |倒序浏览
代码引用地址:http://bbs.chinaunix.net/viewthr ... 3%26filter%3Ddigest

hello.c:

#ifndef __KERNEL__
        #define __KERNEL__
#endif
#ifndef MODULE
        #define MODULE
#endif
#include <linux/init.h>;
#include <linux/module.h>;
#include <linux/kernel.h>;

static int hello_init(void)
{
        printk(KERN_ALERT "Hello, The fucking crazy world\n";
        return 0;
}

static void hello_exit(void)
{
        printk(KERN_ALERT "Bye, The fucking crazy world!\n";
}

module_init(hello_init);
module_exit(hello_exit);

Makefile:
EXEC:=hello
OBJC:=hello.o
CC:=g++
INCLUDE=/home/xxu/embedded/kernel/linux-2.6.17.13/include/linux
$(EXEC)(OBJC)
        $(CC) -g $(OBJC) -o $(EXEC)
$(OBJC):hello.c
        $(CC) -g -c hello.c -I $(INCLUDE)
clean:
        rm -f *.o $(EXEC)

make 后报的错是:g++ -g -c hello.c -I /home/xxu/embedded/kernel/linux-2.6.17.13/include/linux
hello.c:7:24: warning: extra tokens at end of #include directive
hello.c:7:25: error: linux/init.h: No such file or directory
hello.c:8:26: warning: extra tokens at end of #include directive
hello.c:8:27: error: linux/module.h: No such file or directory
hello.c:9:26: warning: extra tokens at end of #include directive
hello.c:9:27: error: linux/kernel.h: No such file or directory
hello.c: In function 'int hello_init()':
hello.c:13: error: 'KERN_ALERT' was not declared in this scope
hello.c:13: error: 'printk' was not declared in this scope
hello.c: In function 'void hello_exit()':
hello.c:19: error: 'KERN_ALERT' was not declared in this scope
hello.c:19: error: 'printk' was not declared in this scope
hello.c: At global scope:
hello.c:22: error: expected constructor, destructor, or type conversion before '(' token
hello.c:23: error: expected constructor, destructor, or type conversion before '(' token
make: *** [hello.o] Error 1

我的内核源代码的地址是:/home/xxu/embedded/kernel/linux-2.6.17.13
高手能指点一下什么地方出错了吗?
大恩不言谢

论坛徽章:
0
2 [报告]
发表于 2006-09-26 09:54 |只看该作者
似乎 include语句不必加 ;

论坛徽章:
0
3 [报告]
发表于 2006-09-26 10:11 |只看该作者
可是不加INCLUDE怎么找头文件了?

论坛徽章:
0
4 [报告]
发表于 2006-09-26 10:24 |只看该作者
原帖由 xiongxu0608 于 2006-9-26 09:43 发表
代码引用地址:http://bbs.chinaunix.net/viewthr ... 3%26filter%3Ddigest

hello.c:

#ifndef __KERNEL__
        #define __KERNEL__
#endif
#ifndef MODUL ...


请检查你的头文件路径,最后是否多了一个linux/子目录,也就是说,一般指到incude 就可以了,你检查一下你这样指是否是必须的

论坛徽章:
0
5 [报告]
发表于 2006-09-26 10:47 |只看该作者
把头文件修改成:
INCLUDE=/home/xxu/embedded/kernel/linux-2.6.17.13/include
结果冒出N多的错?

论坛徽章:
0
6 [报告]
发表于 2006-09-26 10:58 |只看该作者
hello.c:25: error: expected constructor, destructor, or type conversion before '(' token
hello.c:26: error: expected constructor, destructor, or type conversion before '(' token
出这两个错误是什么原因啊?

论坛徽章:
0
7 [报告]
发表于 2006-09-26 11:05 |只看该作者
首先第一个错误:
#include <linux/init.h>;
#include <linux/module.h>;
#include <linux/kernel.h>;
你看到哪里的#include语句后面还带';'的啊?

第二,你已经在INCLUDE中包含了linux目录,你就不能再在#include中包含了

最后我建议你先在google上找一个,可以运行的例子,然后再自己写.你首先要把别人的例子运行起来.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP