免费注册 查看新帖 |

Chinaunix

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

hello驱动编写,不用Makefile可以吗? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-06-30 00:03 |只看该作者 |倒序浏览
*/
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.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);

以上为源程序,编译输入  gcc -o hell hello.c
出错
hello.c: In function `hello_init':
hello.c:11: error: `KERN_ALERT' undeclared (first use in this function)
hello.c:11: error: (Each undeclared identifier is reported only once
hello.c:11: error: for each function it appears in.)
hello.c:11: error: syntax error before string constant
hello.c: In function `hello_exit':
hello.c:17: error: `KERN_ALERT' undeclared (first use in this function)
hello.c:17: error: syntax error before string constant

用Makefile可以通过,文件如下
ifeq ($(KERNELRELEASE),)
    KERNELDIR ?= /lib/modules/$(shell uname -r)/build
     PWD := $(shell pwd)

modules:
        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules

modules_install:
        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install

clean:
        rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions

.PHONY: modules modules_install clean

else
   
    obj-m := hello.o
endif

不用Makefile,怎么用命令去编译?以上的Makefile,谁能帮解释下(是不是自动生成了依赖性,obj-m := hello.o),等待高手解答??

论坛徽章:
0
2 [报告]
发表于 2008-06-30 08:46 |只看该作者
可以
但是稍大一點的工程沒有makefile是很困難的,建議樓主抽時間學習一下

GNU make中文手册PDF文档 v1.5 已经提供下载
http://xhbdahai.cublog.cn/

论坛徽章:
5
摩羯座
日期:2014-07-22 09:03:552015元宵节徽章
日期:2015-03-06 15:50:392015亚冠之大阪钢巴
日期:2015-06-12 16:01:352015年中国系统架构师大会
日期:2015-06-29 16:11:2815-16赛季CBA联赛之四川
日期:2018-12-17 14:10:21
3 [报告]
发表于 2008-06-30 09:28 |只看该作者
首先,你编译的命令都是错的
其次,还是用Makefile吧

论坛徽章:
0
4 [报告]
发表于 2008-06-30 09:32 |只看该作者
原帖由 T-bagwell 于 2008-6-30 09:28 发表
首先,你编译的命令都是错的
其次,还是用Makefile吧


T-Beg兄總結的真精練

论坛徽章:
34
亥猪
日期:2015-03-20 13:55:11戌狗
日期:2015-03-20 13:57:01酉鸡
日期:2015-03-20 14:03:56未羊
日期:2015-03-20 14:18:30子鼠
日期:2015-03-20 14:20:14丑牛
日期:2015-03-20 14:20:31辰龙
日期:2015-03-20 14:35:34巳蛇
日期:2015-03-20 14:35:56操作系统版块每日发帖之星
日期:2015-11-06 06:20:00操作系统版块每日发帖之星
日期:2015-11-08 06:20:00操作系统版块每日发帖之星
日期:2015-11-19 06:20:00黄金圣斗士
日期:2015-11-24 10:43:13
5 [报告]
发表于 2008-06-30 10:45 |只看该作者
gccl hello.c -o hello ?

论坛徽章:
5
摩羯座
日期:2014-07-22 09:03:552015元宵节徽章
日期:2015-03-06 15:50:392015亚冠之大阪钢巴
日期:2015-06-12 16:01:352015年中国系统架构师大会
日期:2015-06-29 16:11:2815-16赛季CBA联赛之四川
日期:2018-12-17 14:10:21
6 [报告]
发表于 2008-06-30 11:06 |只看该作者
驱动的编译方法和普通的应用程序编译的方法还是有点区别的

论坛徽章:
0
7 [报告]
发表于 2008-06-30 13:14 |只看该作者

回复 #1 wgqjjq 的帖子

不写MAKEFILE也能编译,修改/$target/KCONFIG 和 MAKEFILE 添加你需要编译的文件好了

论坛徽章:
0
8 [报告]
发表于 2008-07-01 15:07 |只看该作者
原帖由 T-bagwell 于 2008-6-30 11:06 发表
驱动的编译方法和普通的应用程序编译的方法还是有点区别的


恩~驱动编译用的kernel makefile,跟普通makefile不一样,LZ还是应该用makefile尤其是hello world这样的程序更是应该研究下如何使用makefile

论坛徽章:
0
9 [报告]
发表于 2008-07-03 22:49 |只看该作者
编译内核程序和编译用户空间的应用程序是不一样的,编译内核程序要用要KERNEL里的Makfile。
我是刚刚学习,也遇到过你的问题,后来想明白了。嘿嘿!!

论坛徽章:
0
10 [报告]
发表于 2008-09-10 14:36 |只看该作者
首先你打开ldd3的p29,读一下那个makedile,了解其makefile的原理,编译驱动和编译应用程序完全不同,编译驱动需要用到linux内核源代码树。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP