失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > Ubuntu搭建嵌入式开发(交叉编译)环境-转

Ubuntu搭建嵌入式开发(交叉编译)环境-转

时间:2020-02-03 17:08:43

相关推荐

Ubuntu搭建嵌入式开发(交叉编译)环境-转

大家都比较熟悉gcc编译家族了,但是交叉编译到arm平台的代码是不直接使用gcc的,需要类似名字的一个程序。

1、首先写一个简单的c程序,helloArm.c, 如下:

1 /* 2 * ===================================================================================== 3 * 4 * Filename: helloArm.c 5 * 6 * Description: 7 * 8 * Version: 1.0 9 * Created: 10/23/ 10:36:00 AM10 * Revision: none11 * Compiler: gcc12 *13 *Author: QuNengrong (QNR), Quner8@14 * Company: LordyWorkshop15 *16 * =====================================================================================17 */18 #include <stdio.h>19 int main(){20printf("hello, arm\n");21return 0;22 }

2、查看gcc编译手册,进行初步编译尝试:

man gcc

-mcpu=nameThis specifies the name of the target ARM processor. GCC uses this name to determine what kind of instructions it can emit whengenerating assembly code. Permissible names are: arm2, arm250, arm3, arm6, arm60, arm600, arm610, arm620, arm7, arm7m, arm7d,arm7dm, arm7di, arm7dmi, arm70, arm700, arm700i, arm710, arm710c, arm7100, arm720, arm7500, arm7500fe, arm7tdmi, arm7tdmi-s,arm710t, arm720t, arm740t, strongarm, strongarm110, strongarm1100, strongarm1110, arm8, arm810, arm9, arm9e, arm920, arm920t,arm922t, arm946e-s, arm966e-s, arm968e-s, arm926ej-s, arm940t, arm9tdmi, arm10tdmi, arm1020t, arm1026ej-s, arm10e, arm1020e,arm1022e, arm1136j-s, arm1136jf-s, mpcore, mpcorenovfp, arm1156t2-s, arm1156t2f-s, arm1176jz-s, arm1176jzf-s, cortex-a5,cortex-a7, cortex-a8, cortex-a9, cortex-a15, cortex-r4, cortex-r4f, cortex-r5, cortex-m4, cortex-m3, cortex-m1, cortex-m0,xscale, iwmmxt, iwmmxt2, ep9312, fa526, fa626, fa606te, fa626te, fmp626, fa726te.-mcpu=generic-arch is also permissible, and is equivalent to -march=arch -mtune=generic-arch. See -mtune for more information.-mcpu=native causes the compiler to auto-detect the CPU of the build computer. At present, this feature is only supported onLinux, and not all architectures are recognized. If the auto-detect is unsuccessful the option has no effect.-mtune=nameThis option is very similar to the -mcpu= option, except that instead of specifying the actual target processor type, and hencerestricting which instructions can be used, it specifies that GCC should tune the performance of the code as if the target wereof the type specified in this option, but still choosing the instructions that it will generate based on the CPU specified by a-mcpu= option. For some ARM implementations better performance can be obtained by using this option.-mtune=generic-arch specifies that GCC should tune the performance for a blend of processors within architecture arch. The aimis to generate code that run well on the current most popular processors, balancing between optimizations that benefit some CPUsin the range, and avoiding performance pitfalls of other CPUs. The effects of this option may change in future GCC versions asCPU models come and go.-mtune=native causes the compiler to auto-detect the CPU of the build computer. At present, this feature is only supported onLinux, and not all architectures are recognized. If the auto-detect is unsuccessful the option has no effect.-march=nameThis specifies the name of the target ARM architecture. GCC uses this name to determine what kind of instructions it can emitwhen generating assembly code. This option can be used in conjunction with or instead of the -mcpu= option. Permissible namesare: armv2, armv2a, armv3, armv3m, armv4, armv4t, armv5, armv5t, armv5e, armv5te, armv6, armv6j, armv6t2, armv6z, armv6zk,armv6-m, armv7, armv7-a, armv7-r, armv7-m, iwmmxt, iwmmxt2, ep9312.

主要的参数是 -march= ;尝试编译:

$gcc -march=armv6zk -S helloArm.chelloArm.c:1:0: error: bad value (armv6zk) for -march= switch

出现错误了。。。 gcc手册中不是有着个参数么?怎么无法使用? 然后网上搜索交叉编译的软件,结果发现需要一个专用的gcc, 名为gcc-arm-linux-gnueabi。

在ubuntu下安装非常方便,如下:

$sudo apt-get install gcc-arm-linux-gnueabi

3、使用专用的gcc进行编译即可:

$arm-linux-gnueabi-gcc -S helloArm.c

使用-S只生成汇编文件,可用于分析;得到默认编译后的helloArm.s版本:

1 .arch armv5t 2 .fpu softvfp 3.eabi_attribute 20, 1 4.eabi_attribute 21, 1 5.eabi_attribute 23, 3 6.eabi_attribute 24, 1 7.eabi_attribute 25, 1 8.eabi_attribute 26, 2 9.eabi_attribute 30, 610.eabi_attribute 34, 011.eabi_attribute 18, 412.file "helloArm.c"13 .section .rodata14.align 215 .LC0:16.ascii "hello, arm\000"17 .text18.align 219 .global main20 .type main, %function21 main:22@ args = 0, pretend = 0, frame = 023@ frame_needed = 1, uses_anonymous_args = 024 stmfd sp!, {fp, lr}25add fp, sp, #426 ldr r0, .L327 bl puts28mov r3, #029mov r0, r330 ldmfd sp!, {fp, pc}31 .L4:32.align 233 .L3:34 .word .LC035 .size main, .-main36.ident "GCC: (Ubuntu/Linaro 4.7.2-1ubuntu1) 4.7.2"37.section .note.GNU-stack,"",%progbits

看来默认是armv5t的平台,我买了一个开发板,是OK6410的,采用三星的S3C6410处理器,ARM1176JZF-S内核,主频533MHz/667MHz, 查看上一篇文章转载的各个arm体系对应的指令集,该处理器应该设置 -march=,编译如下:

$arm-linux-gnueabi-gcc -march=armv6zk -S helloArm.c

结果:

1.arch armv6zk;体系结构 2 .fpu softvfp 3.eabi_attribute 20, 1 4.eabi_attribute 21, 1 5.eabi_attribute 23, 3 6.eabi_attribute 24, 1 7.eabi_attribute 25, 1 8.eabi_attribute 26, 2 9.eabi_attribute 30, 610.eabi_attribute 34, 111.eabi_attribute 18, 412.file "helloArm.c"13 .section .rodata14.align 215 .LC0:16.ascii "hello, arm\000"17 .text18.align 219 .global main20 .type main, %function21 main:22@ args = 0, pretend = 0, frame = 023@ frame_needed = 1, uses_anonymous_args = 024 stmfd sp!, {fp, lr}25add fp, sp, #426 ldr r0, .L327 bl puts28mov r3, #029mov r0, r330 ldmfd sp!, {fp, pc}31 .L4:32.align 233 .L3:34 .word .LC035 .size main, .-main36.ident "GCC: (Ubuntu/Linaro 4.7.2-1ubuntu1) 4.7.2"37.section .note.GNU-stack,"",%progbits

到此,相应开发套件已经可用了,如果要生成对应二进制文件,跟使用常规的gcc一样,直接-o helloArm即可,如:

$arm-linux-gnueabi-gcc -o helloArm helloArm.c

希望对大家有用;也欢迎大家多多指点和讨论;

作者:逸云沙鸥

出处:/QuLory/

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

如果觉得《Ubuntu搭建嵌入式开发(交叉编译)环境-转》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。