Linux驱动hello world

编译环境 ubuntu 10.10

交叉编译器 gcc version 4.2.0 20070413 (prerelease) (CodeSourcery Sourcery G++ Lite 2007q1-10)

编译版本 linux 2.6.30

//hello.c
#include <linux/init.h>
#include <linux/module.h> 

static int __init hello_init(void)
{
    printk(KERN_ALERT "Hello,Canson\n");
    return 0;
}

static void __exit hello_exit(void)
{
    printk(KERN_ALERT "Goodbye,Canson!\n Love Linux !Love ARM !\n");
}

module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("Canson"); 
//Makefile
CROSS_COMPILE = arm-none-linux-gnueabi-
CC = $(CROSS_COMPILE)gcc
obj-m :=hello.o

#KERNELDIR ?=/lib/modules/$(shell uname -r)/build
#linux代码所在文件夹
KERNELDIR := /home/canson/armproject/linux-2.6.30
PWD :=$(shell pwd)

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

clean:
    rm -rf *.o *.ko *.mod.c

.PHONY:modules clean
 
 
 
 

canson@ubuntu:~/armproject/driverstudy/helloDriver$ make

make -C /home/canson/armproject/linux-2.6.30 M=/home/canson/armproject/driverstudy/helloDriver modules

make[1]: 正在进入目录 `/home/canson/armproject/linux-2.6.30'

CC [M] /home/canson/armproject/driverstudy/helloDriver/hello.o

cc1: error: unrecognized command line option "-m64"

cc1: error: unrecognized command line option "-mno-red-zone"

cc1: error: unrecognized command line option "-mcmodel=kernel"

cc1: error: unrecognized command line option "-maccumulate-outgoing-args"

cc1: error: unrecognized command line option "-mno-sse"

cc1: error: unrecognized command line option "-mno-mmx"

cc1: error: unrecognized command line option "-mno-sse2"

cc1: error: unrecognized command line option "-mno-3dnow"

cc1: error: unrecognized command line option "-Wframe-larger-than=1024"

cc1: error: unrecognized command line option "-fno-dwarf2-cfi-asm"

make[2]: *** [/home/canson/armproject/driverstudy/helloDriver/hello.o] 错误 1

make[1]: *** [_module_/home/canson/armproject/driverstudy/helloDriver] 错误 2

make[1]:正在离开目录 `/home/canson/armproject/linux-2.6.30'

make: *** [modules] 错误 2

 

如此错误,google得知是由于本身是交叉编译,但是却没有指定体系的结果

于是

 

canson@ubuntu:~/armproject/driverstudy/helloDriver$ export ARCH=arm

canson@ubuntu:~/armproject/driverstudy/helloDriver$ make

make -C /home/canson/armproject/linux-2.6.30 M=/home/canson/armproject/driverstudy/helloDriver modules

make[1]: 正在进入目录 `/home/canson/armproject/linux-2.6.30'

CC [M] /home/canson/armproject/driverstudy/helloDriver/hello.o

Building modules, stage 2.

MODPOST 1 modules

CC /home/canson/armproject/driverstudy/helloDriver/hello.mod.o

LD [M] /home/canson/armproject/driverstudy/helloDriver/hello.ko

ld: /home/canson/armproject/driverstudy/helloDriver/hello.o: Relocations in generic ELF (EM: 40)

ld: /home/canson/armproject/driverstudy/helloDriver/hello.o: Relocations in generic ELF (EM: 40)

ld: /home/canson/armproject/driverstudy/helloDriver/hello.o: Relocations in generic ELF (EM: 40)

ld: /home/canson/armproject/driverstudy/helloDriver/hello.o: Relocations in generic ELF (EM: 40)

/home/canson/armproject/driverstudy/helloDriver/hello.o: could not read symbols: File in wrong format

make[2]: *** [/home/canson/armproject/driverstudy/helloDriver/hello.ko] 错误 1

make[1]: *** [modules] 错误 2

make[1]:正在离开目录 `/home/canson/armproject/linux-2.6.30'

make: *** [modules] 错误 2

指定了编译体系之后,还是提示有错误

这次是由于GCC是交叉 编译器,但是LD却还是PC的缘故

canson@ubuntu:~/armproject/driverstudy/helloDriver$ export CROSS_COMPILE=arm-none-linux-gnueabi-

canson@ubuntu:~/armproject/driverstudy/helloDriver$ make

make -C /home/canson/armproject/linux-2.6.30 M=/home/canson/armproject/driverstudy/helloDriver modules

make[1]: 正在进入目录 `/home/canson/armproject/linux-2.6.30'

CC [M] /home/canson/armproject/driverstudy/helloDriver/hello.o

Building modules, stage 2.

MODPOST 1 modules

CC /home/canson/armproject/driverstudy/helloDriver/hello.mod.o

LD [M] /home/canson/armproject/driverstudy/helloDriver/hello.ko

make[1]:正在离开目录 `/home/canson/armproject/linux-2.6.30'

编译成功

经验教训:Makefile中的 obj-m,不知道为什么,老是打成ojb-m,所以造成了不能编译的假象.但是又没有错误提示.

然后就是需要指定体系和指定CROSS_COMPILE,这点不是很明白,因为我已经在Makefile中指定了交叉编译器了.可是为什么还是需要再一次指定呢…

posted @ 2011-04-19 10:44  CanY  Views(5020)  Comments(0Edit  收藏  举报