通用交叉编译工具链
除了板卡厂商提供的交叉编译工具链外,还存在通用的交叉编译工具链。常用的有:arm-linux-gnueabihf-gcc和arm-none-linux-gnueabi-gcc。
Ubuntu命令安装
$ sudo apt-get install g++-5-arm-linux-gnueabihf g++-5-arm-linux-gnueabihf $ sudo update-alternatives --install /usr/bin/arm-linux-gnueabihf-gcc arm-linux-gnueabihf-gcc /usr/bin/arm-linux-gnueabihf-gcc-5 100 --slave /usr/bin/arm-linux-gnueabihf-g++ arm-linux-gnueabihf-g++ /usr/bin/arm-linux-gnueabihf-g++-5 ## 更新软连接
arm-linux-gnueabihf-gcc二进制安装
arm-linux-gnueabihf-gcc是由 Linaro 公司基于GCC推出的的ARM交叉编译工具。可用于交叉编译ARM系统中所有环节的代码,包括裸机程序、u-boot、Linux kernel、filesystem和App应用程序。
arm-linux-gneabihf-gcc 与 CodeSourcery 的 arm-none-linux-gnueabi-gcc 类 似,都可以用来编译基于ARM平台所有环节代码,CodeSourcery 的交叉编译工具发布较早,从2005年开始一直到现在。
Linaro 则是由ARM、飞思卡尔、IBM、Samsung、ST-Ericsson和TI等厂商联合,为开发不同半导体公司系统单芯片(SoC)平台的共通软件的 一个非盈利公司。
Linaro于2010年成立,并于当年11月发布第一版ARM Cortex-A 为核心的 SoC 进行效能优化的软件工具。
https://www.linaro.org/downloads/
https://releases.linaro.org/components/toolchain/binaries/latest-7/arm-linux-gnueabihf/
1. 下载软件包
从linaro的网站下载预编译二进制包,地址:https://launchpad.net/linaro-toolchain-binaries/trunk/2013.10。
注意选择的版本哦,我们要使用linux下的哦。选择这个:gcc-linaro-arm-linux-gnueabihf-4.8-2013.10_linux.tar.bz2
2. 解压
解压gcc-linaro-arm-linux-gnueabihf-4.8-2013.10_linux.tar.xz 到 ~/arm-cross-toolchain/目录下
3. 设置环境变量
~$ vi .bashrc
在最后添加如下 2 行:
PATH=$PATH:/home/lxl/arm-cross-toolchain/gcc-linaro-arm-linux-gnueabihf-4.8-2013.10_linux/bin
export PATH
请注意,第一行的$PATH后面是英文冒号,而冒号后面是你的cross-toolchain的可执行文件目录(bin目录)的绝对路径。 这两句的意思就是将cross-toolchain的可执行文件路径加入系统环境变量PATH中。
4. 使环境变量 生效
~$ source .bashrc
5. 测试
~$ arm-linux-gnueabihf-gcc -v
报错:arm-linux-gnueabihf-gcc: error while loading shared libraries: libstdc++.so.6: cannot open shared object file: No such file or directory
原因:64位的系统,缺少 lib32stdc++6这个包
解决:~$ sudo apt-get install lib32stdc++6
浮点运算
imx6ul集成了硬件浮点运算,编译时可使能硬件浮点:
-march=armv7-a -mfpu=neon -mfloat-abi=hard
arm-linux-gnueabihf-gcc -march=armv7-a -mfpu=neon -mfloat-abi=hard app.c -o app
交叉编译示例
#!/bin/bash TOPDIR=$(pwd) BASEDIR=package BUILDDIR=build_dir DEPLOY_TAR=deploy.tar.gz rm winstall log_post/wbuild ${BASEDIR}/${BUILDDIR}/* -rf ################ 1. compile base package #compile curl cd ${TOPDIR}/${BASEDIR} MODULE_DIR=curl-7.70.0 tar xvf ${MODULE_DIR}.tar.* -C ${BUILDDIR} cd ${BUILDDIR}/${MODULE_DIR} mkdir wbuild cd wbuild cmake -DCMAKE_TOOLCHAIN_FILE=${TOPDIR}/${BASEDIR}/CrossCompile.cmake \ -DCMAKE_BUILD_TYPE=release -DCMAKE_INSTALL_PREFIX=${TOPDIR}/winstall .. make make install #compile mosquitto cd ${TOPDIR}/${BASEDIR} MODULE_DIR=mosquitto-1.6.12 tar xvf ${MODULE_DIR}.tar.* -C ${BUILDDIR} cd ${BUILDDIR}/${MODULE_DIR} mkdir wbuild cd wbuild cmake -DCMAKE_TOOLCHAIN_FILE=${TOPDIR}/${BASEDIR}/CrossCompile.cmake \ -DCMAKE_BUILD_TYPE=release -DCMAKE_INSTALL_PREFIX=${TOPDIR}/winstall \ -DWITH_SRV=no -DWITH_TLS=yes -DDOCUMENTATION=no -DWITH_WEBSOCKETS=no .. make make install #compile sqlite3 cd ${TOPDIR}/${BASEDIR} MODULE_DIR=sqlite-autoconf-3340000 tar xvf ${MODULE_DIR}.tar.* -C ${BUILDDIR} cd ${BUILDDIR}/${MODULE_DIR} CC="/opt/fsl-imx-x11/4.1.15-2.1.0/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-gcc -march=armv7ve -mfpu=neon -mfloat-abi=hard -mcpu=cortex-a7 --sysroot=/opt/fsl-imx-x11/4.1.15-2.1.0/sysroots/cortexa7hf-neon-poky-linux-gnueabi" \ ./configure --prefix=${TOPDIR}/winstall \ --host=arm-poky-linux-gnueabi \ --with-sysroot=/opt/fsl-imx-x11/4.1.15-2.1.0/sysroots/cortexa7hf-neon-poky-linux-gnueabi make make install #compile FFmpeg cd ${TOPDIR}/${BASEDIR} MODULE_DIR=x264-snapshot-20191217-2245-stable tar xvf ${MODULE_DIR}.tar.* -C ${BUILDDIR} cd ${BUILDDIR}/${MODULE_DIR} ./configure --prefix=${TOPDIR}/winstall \ --host=arm-poky-linux-gnueabi --sysroot=/opt/fsl-imx-x11/4.1.15-2.1.0/sysroots/cortexa7hf-neon-poky-linux-gnueabi \ --cross-prefix="/opt/fsl-imx-x11/4.1.15-2.1.0/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-" \ --extra-cflags="-march=armv7ve -mfpu=neon -mfloat-abi=hard -mcpu=cortex-a7" \ --enable-shared --enable-strip --disable-asm --disable-cli make make install #compile FFmpeg cd ${TOPDIR}/${BASEDIR} MODULE_DIR=FFmpeg-n3.4.8 tar xvf ${MODULE_DIR}.tar.* -C ${BUILDDIR} cd ${BUILDDIR}/${MODULE_DIR} ./configure --prefix=${TOPDIR}/winstall \ --enable-gpl --enable-shared --disable-static --disable-doc --cross-prefix=arm-poky-linux-gnueabi- \ --cc="/opt/fsl-imx-x11/4.1.15-2.1.0/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-gcc -march=armv7ve -mfpu=neon -mfloat-abi=hard -mcpu=cortex-a7 --sysroot=/opt/fsl-imx-x11/4.1.15-2.1.0/sysroots/cortexa7hf-neon-poky-linux-gnueabi" \ --strip="/opt/fsl-imx-x11/4.1.15-2.1.0/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-strip" \ --enable-cross-compile --arch=arm --target-os=linux --enable-nonfree --enable-pthreads --enable-libx264 make make install # compile task daemon MODULE_DIR=task_start cd ${TOPDIR}/${MODULE_DIR} make clean make INSTALL_DIR=${TOPDIR}/winstall make install # compile log MODULE_DIR=log_post cd ${TOPDIR}/${MODULE_DIR} mkdir wbuild cd wbuild cmake -DCMAKE_TOOLCHAIN_FILE=${TOPDIR}/${MODULE_DIR}/CrossCompile.cmake \ -DCMAKE_BUILD_TYPE=release -DCMAKE_INSTALL_PREFIX=${TOPDIR}/winstall .. make make install ################ 2. deploy cd ${TOPDIR}/winstall tar -cvf ${DEPLOY_TAR} bin etc lib sbin TAR_MD5=`md5sum ${DEPLOY_TAR}` echo ${TAR_MD5} #scp ${DEPLOY_TAR} root@client_ip:/tmp #ssh root@client_ip #cd /tmp #tar --no-same-owner -xvf ${DEPLOY_TAR} -C /usr/local exit 0
参考: