树莓派2 raspyberry Pi2 交叉编译app

Pi 使用的是ARMV7架构的BCM2836, 下载交叉编译器 arm-linux-gnueabihf-gcc 即可.

本地环境: Ubuntu14 + x86_64

1. 下载编译器地址:

1). linaro开源组织有相关的交叉工具链下载,点击进入网页选择下载即可,地址:https://launchpad.net/linaro-toolchain-binaries/+download

2). 上述网址网速较慢, 总是下载失败. 找了国内的网址 http://pan.baidu.com/s/1pJ2rdkZ

 

2. 下载完后, 解压, 并将 gcc-linaro-arm-linux-gnueabihf-4.9-2014.07_linux/bin 目录设置到系统环境变量中.

gcc-linaro-arm-linux-gnueabihf-4.9-2014.07_linux/bin$ file arm-linux-gnueabihf-gcc-4.9.1
arm-linux-gnueabihf-gcc-4.9.1: ELF 32-bit LSB  executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.15, stripped

这里下载的交叉编译器的是32位的, 确保你的系统支持32位程序的运行.

3. 准备测试代码 main.c:

#include <stdio.h>
int main()
{
    printf("hellow world!\n");
}

4. 编译

arm-linux-gnueabihf-gcc main.c -v

详细交叉编译选项为:COLLECT_GCC_OPTIONS='-v' '-march=armv7-a' '-mtune=cortex-a9' '-mfloat-abi=hard' '-mfpu=vfpv3-d16' '-mthumb' '-mtls-dialect=gnu'

编译结果:

file a.out
a.out: ELF 32-bit LSB  executable, ARM, EABI5 version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 3.1.1, BuildID[sha1]=3103ff5892a051b7e185e8ae88bf6cb997af7781, not stripped

5. 执行

上传到 pi 上, 执行结果如下:

pi@raspberrypi ~ $ ./a.out
hellow world!
pi@raspberrypi ~ $

 

posted @ 2015-10-04 23:07  kunzj  阅读(696)  评论(0编辑  收藏  举报