免费注册 查看新帖 |

Chinaunix

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

[驱动] 如何用arm-linux-gcc编译驱动程序,Makefile文件怎么写? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-04-09 09:28 |只看该作者 |倒序浏览
本人是新手,前天编写一个最简单驱动到嵌入式ARM板上时出现了错误,然后试了很多方法都不成功,不知道怎么写和配置。请各位先辈指点一下。

论坛徽章:
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
2 [报告]
发表于 2011-04-09 11:52 |只看该作者
参考本版精华贴,肯定会找到满意的回答

论坛徽章:
0
3 [报告]
发表于 2011-04-09 16:28 |只看该作者
回复 2# T-Bagwell


    没有啊,哪一个有?我就是不明白怎么移植。我是这样写的:刚开始我直接参考桌面平台的写Makefile:
KVERS = $(shell uname -r)

# Kernel modules
obj-m += hello.o

# Specify flags for the module compilation.
#EXTRA_CFLAGS=-g -O0

build: kernel_modules

kernel_modules:
make -C /lib/modules/$(KVERS)/build M=$(CURDIR) modules

clean:
make -C /lib/modules/$(KVERS)/build M=$(CURDIR) clean


在PC上编译生成了.ko文件,然后我下载到ARM板上运行insmod hello.ko时出现错误:can't insert 'hello.ko':invalid module format .

我就想不通,我想可能要修改什么配置吧,然后网上搜索看到要这样修改:在内核源代码目录下修改Makefile:

找到下列行:   
              ARCH                         ?=$(SUBARCH)
              CROSS_COMPILE         ?=
              改成:
              ARCH         :=   arm
              CROSS_COMPILE   =/usr/local/arm-linux/bin/arm-linux-gcc

这样把系统添加对ARM的支持。
但是这样编译时却没有通过了。。。
最后我在一些资料上看到一个现成的用arm-linux-gcc编译的驱动的Makefile文件,这样的:

ifneq ($(KERNELRELEASE),)
obj-m:=hello.o
else
KDIR:=/lib/modules/$(shell uname -r)/build
all:
        make -C $(KDIR) M=$(PWD) modules ARCH=arm CROSS_COMPILE=arm-linux-
clean:
        rm -f *.ko *.o *.mod.o *.mod.c *.symvers
endif

我看到以为找到了,便仿照之用来编译,但是make却又出现如下错误:

[root@FedoraFor2440 cs]# make
make -C /lib/modules/2.6.27.5-117.fc10.i686/build M=/home/admin/cs modules ARCH=arm CROSS_COMPILE=arm-linux-
make[1]: Entering directory `/usr/src/kernels/2.6.27.5-117.fc10.i686'
  CC [M]  /home/admin/cs/hello.o
In file included from include/asm/system.h:10,
                 from include/asm/processor.h:17,
                 from include/linux/prefetch.h:14,
                 from include/linux/list.h:6,
                 from include/linux/module.h:9,
                 from /home/admin/cs/hello.c:9:
include/linux/kernel.h:195: warning: 'regparm' attribute directive ignored
include/linux/kernel.h:197: warning: 'regparm' attribute directive ignored
include/linux/kernel.h:217: warning: 'regparm' attribute directive ignored
In file included from include/linux/thread_info.h:47,
                 from include/linux/preempt.h:9,
                 from include/linux/spinlock.h:50,
                 from include/linux/seqlock.h:29,
                 from include/linux/time.h:8,
                 from include/linux/stat.h:60,
                 from include/linux/module.h:10,
                 from /home/admin/cs/hello.c:9:
include/asm/thread_info.h:174: error: invalid register name for 'current_stack_pointer'
In file included from include/linux/rwsem.h:22,
                 from include/linux/notifier.h:14,
                 from include/linux/memory_hotplug.h:6,
                 from include/linux/mmzone.h:560,
                 from include/linux/gfp.h:4,
                 from include/linux/kmod.h:22,
                 from include/linux/module.h:13,
                 from /home/admin/cs/hello.c:9:
include/asm/rwsem.h:48: warning: 'regparm' attribute directive ignored
include/asm/rwsem.h:50: warning: 'regparm' attribute directive ignored
include/asm/rwsem.h:52: warning: 'regparm' attribute directive ignored
include/asm/rwsem.h:54: warning: 'regparm' attribute directive ignored
In file included from include/asm/smp.h:12,
                 from include/linux/smp.h:28,
                 from include/linux/topology.h:33,
                 from include/linux/mmzone.h:683,
                 from include/linux/gfp.h:4,
                 from include/linux/kmod.h:22,
                 from include/linux/module.h:13,
                 from /home/admin/cs/hello.c:9:
include/asm/mpspec.h:9:25: error: mach_mpspec.h: No such file or directory
In file included from include/asm/smp.h:12,
                 from include/linux/smp.h:28,
                 from include/linux/topology.h:33,
                 from include/linux/mmzone.h:683,
                 from include/linux/gfp.h:4,
                 from include/linux/kmod.h:22,
                 from include/linux/module.h:13,
                 from /home/admin/cs/hello.c:9:
include/asm/mpspec.h:39: error: 'MAX_MP_BUSSES' undeclared here (not in a function)
In file included from include/asm/smp.h:15,
                 from include/linux/smp.h:28,
                 from include/linux/topology.h:33,
                 from include/linux/mmzone.h:683,
                 from include/linux/gfp.h:4,
                 from include/linux/kmod.h:22,
                 from include/linux/module.h:13,
                 from /home/admin/cs/hello.c:9:
include/asm/io_apic.h:149: error: 'MAX_IRQ_SOURCES' undeclared here (not in a function)
In file included from include/linux/smp.h:28,
                 from include/linux/topology.h:33,
                 from include/linux/mmzone.h:683,
                 from include/linux/gfp.h:4,
                 from include/linux/kmod.h:22,
                 from include/linux/module.h:13,
                 from /home/admin/cs/hello.c:9:
include/asm/smp.h:187:28: error: mach_apicdef.h: No such file or directory
In file included from include/linux/smp.h:28,
                 from include/linux/topology.h:33,
                 from include/linux/mmzone.h:683,
                 from include/linux/gfp.h:4,
                 from include/linux/kmod.h:22,
                 from include/linux/module.h:13,
                 from /home/admin/cs/hello.c:9:
include/asm/smp.h: In function 'hard_smp_processor_id':
include/asm/smp.h:191: error: implicit declaration of function 'GET_APIC_ID'
make[2]: *** [/home/admin/cs/hello.o] 错误 1
make[1]: *** [_module_/home/admin/cs] 错误 2
make[1]: Leaving directory `/usr/src/kernels/2.6.27.5-117.fc10.i686'
make: *** [all] 错误 2
[root@FedoraFor2440 cs]# make

我就郁闷了,现在也没办法。。。

论坛徽章:
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
4 [报告]
发表于 2011-04-09 17:03 |只看该作者

论坛徽章:
0
5 [报告]
发表于 2011-04-10 18:53 |只看该作者
回复 4# T-Bagwell
http://bbs.chinaunix.net/viewthread.php?tid=1921952 上面说的方法我还是不是很明白啊,我现在简直没办法了,昨天晚我看到一个资料上面写的helloworld驱动的例子有两份Makefile,一份是x86机子上的,一份是arm平台上的,arm上的这样写:

ifneq ($(KERNELRELEASE),)
obj-m:=hello.o
else
KDIR:=/usr/src/kernels/opt/EmbedSky/linux-2.6.30.4/
all:
        make -C $(KDIR) M=$(PWD) modules ARCH=arm CROSS_COMPLIE=arm-linux-
clean:
        rm  -f  *.ko  *.o  *.mod.o  *.mod.c  *.symvers
endif

看到后我想应该是要用用于移植的内核来编译吧,于是我干脆把资料提供的内核拷贝到PC的Linux系统中然后解压、编译直至安装,然后就再模仿例子写了Makefile如上所示,然后make后终于通过编译成功了,然后我再下载到arm板上安装,却又出现下面的错误:insmod: can't insert 'hello.ko': unknown symbol in module, or unknown parameter。我就又彻底晕了。到底是怎么回事,应该怎么办的呢?

论坛徽章:
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 [报告]
发表于 2011-04-10 19:37 |只看该作者
编译器版本?
编译kernel配置的config做成附件发上来看看吧

论坛徽章:
0
7 [报告]
发表于 2011-04-10 20:25 |只看该作者
回复 6# T-Bagwell


    哦,好,编译器好像是Arm-linux-gcc 4.3.2 ,我编译内核的配置是按照开发板提供的《天嵌科技出品Linux移植之step by step》设置的

config.zip

13.91 KB, 下载次数: 475

论坛徽章:
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
8 [报告]
发表于 2011-04-10 20:54 |只看该作者
看上去没啥问题
确认以下几项
1.编译kernel 的编译器和你编译driver的编译器是同一个版本
2.make你的driver的时候有警告信息没有,有的话贴出来,或者make -n,然后把信息贴出来
3.用file命令看看你编译出来的.ko的信息,贴出来
4.把你的操作步骤发一下,大伙看一下

应该不是大问题

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
9 [报告]
发表于 2011-04-10 21:33 |只看该作者
回复 1# psp3000pes



编辑  Makefile  如下。



    obj-m += hello-1.o

all:
        make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
        make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean


然后 make.


如果 是给embedded  系统编译 module,  先把 embedded 系统的kernel 搞好,编译一道。然后  编辑  Makefile  如下。



   obj-m += hello-1.o

all:
        make -C /dirto/kernel/src   M=$(PWD) modules

clean:
        make  -C /dirto/kernel/src   M=$(PWD) clean


然后 make.

评分

参与人数 1可用积分 +4 收起 理由
T-Bagwell + 4 感谢分享

查看全部评分

论坛徽章:
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
10 [报告]
发表于 2011-04-10 21:51 |只看该作者
回复  psp3000pes



编辑  Makefile  如下。



    obj-m += hello-1.o

all:
        make -C /li ...
goldenfort 发表于 2011-04-10 21:33



    补充:最好先make以下kernel
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP