Macbook搭建交叉编译工具以及区别

引用请标明出处:http://blog.csdn.net/callon_h/article/details/51308755

内容均为实践所得。

参考网址:
http://www.51hei.com/bbs/dpj-19125-1.html

具体内容:
首先需要安装macports,分享地址:
http://pan.baidu.com/s/1dFz4GKT
http://www.macports.org/install.php
安装完成后我们使用命令:

callondeMacBook-Pro:~ callon$ sudo port install libelf
Password:
Error: Port libelf not found

居然是没有找到,原因是我们需要更新macports:

callondeMacBook-Pro:~ callon$ sudo port -v selfupdate
--->  Updating MacPorts base sources using rsync
receiving file list ... done
base.tar

sent 31276 bytes  received 223560 bytes  8090.03 bytes/sec
total size is 27013120  speedup is 106.00
receiving file list ... done

--->  MacPorts base is already the latest version

The ports tree has been updated. To upgrade your installed ports, you should run
  port upgrade outdated
callondeMacBook-Pro:~ callon$ 
callondeMacBook-Pro:~ callon$ sudo port upgrade outdated
Password:
Nothing to upgrade.
callondeMacBook-Pro:~ callon$ sudo port search libelf
libelf @0.8.13_2 (devel)
    ELF object file access library

py-pylibelf @0.1.20110915 (python, devel)
    Pythonic wrapper for the libelf library.

py27-pylibelf @0.1.20110915 (python, devel)
    Pythonic wrapper for the libelf library.

Found 3 ports.

此时就说明我们可以正常使用macports安装软件了,下面开始正式操作:

callondeMacBook-Pro:~ callon$ sudo port install libelf

出现:

--->  Installing libelf @0.8.13_2
--->  Activating libelf @0.8.13_2
--->  Cleaning libelf
--->  Updating database of binaries
--->  Scanning binaries for linking errors               
--->  No broken files found.

即为安装成功。
同理:

callondeMacBook-Pro:~ callon$ sudo port install gsed
callondeMacBook-Pro:~ callon$ sudo port install arm-elf-binutils
callondeMacBook-Pro:~ callon$ sudo port install arm-elf-gcc

此时我们的裸机编译环境其实就搭建好了,但是我们需要把裸机makefile的所有之前用的arm-linux-xxx换成现在的arm-elf-xxx即可!

callondeMacBook-Pro:hkl-continue callon$ make clean
rm -f *.o *.elf *.bin
make -C dev clean
rm -f *.o       
make -C lib clean
rm -f *.o       
callondeMacBook-Pro:hkl-continue callon$ make
arm-elf-gcc -g -c start.S
arm-elf-gcc -g -fno-builtin -I/Users/callon/Desktop/hkl-continue/include -c main.c
make -C dev all
arm-elf-gcc -g -fno-builtin -I/Users/callon/Desktop/hkl-continue/include -c mmu.c
arm-elf-gcc -g -fno-builtin -I/Users/callon/Desktop/hkl-continue/include -c interrupt.c
arm-elf-gcc -g -fno-builtin -I/Users/callon/Desktop/hkl-continue/include -c gpio.c
arm-elf-gcc -g -fno-builtin -I/Users/callon/Desktop/hkl-continue/include -c uart.c
arm-elf-gcc -g -fno-builtin -I/Users/callon/Desktop/hkl-continue/include -c nandflash.c
arm-elf-gcc -g -fno-builtin -I/Users/callon/Desktop/hkl-continue/include -c lcd.c
arm-elf-gcc -g -fno-builtin -I/Users/callon/Desktop/hkl-continue/include -c dma.c
arm-elf-gcc -g -fno-builtin -I/Users/callon/Desktop/hkl-continue/include -c bmp.c
arm-elf-gcc -g -fno-builtin -I/Users/callon/Desktop/hkl-continue/include -c bmpp.c
arm-elf-gcc -g -fno-builtin -I/Users/callon/Desktop/hkl-continue/include -c timer.c
arm-elf-gcc -g -fno-builtin -I/Users/callon/Desktop/hkl-continue/include -c touch.c
arm-elf-ld -r -o dev.o mmu.o interrupt.o gpio.o uart.o nandflash.o lcd.o dma.o bmp.o bmpp.o timer.o touch.o
arm-elf-gcc -g -c mem.S
make -C lib all
arm-elf-gcc -g -fno-builtin -I/Users/callon/Desktop/hkl-continue/include -c div64.S
arm-elf-gcc -g -fno-builtin -I/Users/callon/Desktop/hkl-continue/include -c lib1funcs.S
arm-elf-gcc -g -fno-builtin -I/Users/callon/Desktop/hkl-continue/include -c ctype.c
arm-elf-gcc -g -fno-builtin -I/Users/callon/Desktop/hkl-continue/include -c muldi3.c
arm-elf-gcc -g -fno-builtin -I/Users/callon/Desktop/hkl-continue/include -c printf.c
arm-elf-gcc -g -fno-builtin -I/Users/callon/Desktop/hkl-continue/include -c string.c
arm-elf-gcc -g -fno-builtin -I/Users/callon/Desktop/hkl-continue/include -c vsprintf.c
arm-elf-ld -r -o lib.o div64.o lib1funcs.o ctype.o muldi3.o printf.o string.o vsprintf.o
arm-elf-ld -Thkl.lds -o hkl_boot.elf start.o main.o dev/dev.o mem.o lib/lib.o
arm-elf-objcopy -O binary hkl_boot.elf hkl_boot.bin
arm-elf-objdump -D -S hkl_boot.elf > dump
callondeMacBook-Pro:hkl-continue callon$ ls
Makefile    hkl.lds     include     main.o      start.S
dev     hkl_boot.bin    lib     mem.S       start.S~
dump        hkl_boot.elf    main.c      mem.o       start.o

同样的尝试了安装arm-none-eabi-工具链,也能编译裸机代码:

callondeMacBook-Pro:s3c-linux-2.6.28.6-Real6410 callon$ sudo port install arm-none-eabi-gcc

但是编译不了内核,总会出现莫名其妙的问题。

***最后现象总结:***
arm-none-eabi- 能够编译内核模块,能够编译裸机代码,但是在编译应用程序会出错:

callondeMacBook-Pro:key_irq_misc callon$ arm-none-eabi-gcc -static key_app.c -o key_app
key_app.c: 在函数‘main’中:
key_app.c:9:8: 警告:隐式声明函数‘open’ [-Wimplicit-function-declaration]
   fd = open("/dev/6410key",0);
        ^
key_app.c:11:3: 警告:隐式声明函数‘read’ [-Wimplicit-function-declaration]
   read(fd, &key_num, 4);
   ^
key_app.c:15:3: 警告:隐式声明函数‘close’ [-Wimplicit-function-declaration]
   close(fd);
   ^
/opt/local/lib/gcc/arm-none-eabi/5.1.0/../../../../arm-none-eabi/bin/ld: cannot find crt0.o: No such file or directory
collect2: 错误:ld 返回 1

arm-elf- 能够编译出应用程序和裸机代码:

callondeMacBook-Pro:key_irq_misc callon$ arm-elf-gcc -static key_app.c -o key_app
callondeMacBook-Pro:key_irq_misc callon$ ls

但是编译不了内核模块:

callondeMacBook-Pro:key_irq_misc callon$ make
make -C /Users/callon/Documents/s3c-linux-2.6.28.6-Real6410 M=/Users/callon/Desktop/key_irq_misc modules CROSS_COMPILE=arm-elf- ARCH=arm
  CC [M]  /Users/callon/Desktop/key_irq_misc/key.o
/Users/callon/Desktop/key_irq_misc/key.c:1:0: error: FPA is unsupported in the AAPCS
make[2]: *** [/Users/callon/Desktop/key_irq_misc/key.o] Error 1
make[1]: *** [_module_/Users/callon/Desktop/key_irq_misc] Error 2
make: *** [all] Error 2

最后还有一种交叉编译工具:
参考http://blog.csdn.net/rk2900/article/details/8738442

新建一个磁盘映像:
磁盘工具->文件->新建映像->空白映像
Macbook搭建交叉编译工具以及区别_第1张图片
有时你的磁盘映像还是不区分大小写,你可以在磁盘映像下建立两个readme和Readme文件夹来测试,如果的确还是不区分,就磁盘工具选择:磁盘映像->抹掉
Macbook搭建交叉编译工具以及区别_第2张图片
再操作一次就完全正确了,你可以进行测试。
然后将下载好的ARMx-2009q3-67.tar.bz2,链接在参考的blog中有提到,这里再啰嗦一下:
https://github.com/downloads/UnhandledException/ARMx/ARMx-2009q3-67.tar.bz2
拷贝到磁盘映像下,执行命令:
Macbook搭建交叉编译工具以及区别_第3张图片
最后修改/etc/profile把path环境变量修改了,然后source /etc/profile即可!
Macbook搭建交叉编译工具以及区别_第4张图片
最后修改内核代码中.config或者Makefile里对CROSS_COMPILE的定义,改成arm-none-linux-gnueabi-即可,此时就可以进行内核代码编译了:
Macbook搭建交叉编译工具以及区别_第5张图片
Macbook搭建交叉编译工具以及区别_第6张图片
编译完内核再编译内核模块也就不会有莫名其妙的问题了:
Macbook搭建交叉编译工具以及区别_第7张图片
成功!
但是裸机代码拿arm-none-linux-gnueabi- 编译会有问题。

此博文得到的结论均为实践与查找资料所得,仅供参考。

你可能感兴趣的:(mac,linux,嵌入式,交叉编译)