欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

交叉编译笔记

程序员文章站 2022-07-14 20:52:03
...

常常需要交叉编译一些linux系统工具,如下使用elfutils和ltrace举两个例子

ltrace依赖elfutils,所以先编译elfutils

elftuils的makefile配置文件是configure,在不同的交叉编译工具和环境下需要采用不同的configure参数

情况1:iMX6的交叉编译

iMX6已经有做好的环境environment-setup-cortexa9hf-neon-poky-linux-gnueabi,只要source一下该环境变量就能获得对应的值如下变量都已经准备到该终端下的环境里了

export CC="arm-poky-linux-gnueabi-gcc  -march=armv7-a -mfpu=neon  -mfloat-abi=hard -mcpu=cortex-a9 --sysroot=$SDKTARGETSYSROOT"
export CXX="arm-poky-linux-gnueabi-g++  -march=armv7-a -mfpu=neon  -mfloat-abi=hard -mcpu=cortex-a9 --sysroot=$SDKTARGETSYSROOT"
export CPP="arm-poky-linux-gnueabi-gcc -E  -march=armv7-a -mfpu=neon  -mfloat-abi=hard -mcpu=cortex-a9 --sysroot=$SDKTARGETSYSROOT"
export AS="arm-poky-linux-gnueabi-as "
export LD="arm-poky-linux-gnueabi-ld  --sysroot=$SDKTARGETSYSROOT"
export GDB=arm-poky-linux-gnueabi-gdb
export STRIP=arm-poky-linux-gnueabi-strip
export RANLIB=arm-poky-linux-gnueabi-ranlib
export OBJCOPY=arm-poky-linux-gnueabi-objcopy
export OBJDUMP=arm-poky-linux-gnueabi-objdump
export AR=arm-poky-linux-gnueabi-ar
export NM=arm-poky-linux-gnueabi-nm
export M4=m4
export TARGET_PREFIX=arm-poky-linux-gnueabi-
export CONFIGURE_FLAGS="--target=arm-poky-linux-gnueabi --host=arm-poky-linux-gnueabi --build=x86_64-linux --with-libtool-sysroot=$SDKTARGETSYSROOT"
export CFLAGS=" -O2 -pipe -g -feliminate-unused-debug-types -fdebug-prefix-map=/home/system_dir/var/lib/jenkins/workspace/OS-Project/iMX6-Sabresd-Wayland/iMX6-Sabresd/build-wayland/tmp/work/x86_64-nativesdk-pokysdk-linux/meta-environment-imx6qsabresd/1.0-r8=/usr/src/debug/meta-environment-imx6qsabresd/1.0-r8 -fdebug-prefix-map=/home/system_dir/var/lib/jenkins/workspace/OS-Project/iMX6-Sabresd-Wayland/iMX6-Sabresd/build-wayland/tmp/sysroots/x86_64-linux= -fdebug-prefix-map=/home/system_dir/var/lib/jenkins/workspace/OS-Project/iMX6-Sabresd-Wayland/iMX6-Sabresd/build-wayland/tmp/sysroots/x86_64-nativesdk-pokysdk-linux= "
export CXXFLAGS=" -O2 -pipe -g -feliminate-unused-debug-types -fdebug-prefix-map=/home/system_dir/var/lib/jenkins/workspace/OS-Project/iMX6-Sabresd-Wayland/iMX6-Sabresd/build-wayland/tmp/work/x86_64-nativesdk-pokysdk-linux/meta-environment-imx6qsabresd/1.0-r8=/usr/src/debug/meta-environment-imx6qsabresd/1.0-r8 -fdebug-prefix-map=/home/system_dir/var/lib/jenkins/workspace/OS-Project/iMX6-Sabresd-Wayland/iMX6-Sabresd/build-wayland/tmp/sysroots/x86_64-linux= -fdebug-prefix-map=/home/system_dir/var/lib/jenkins/workspace/OS-Project/iMX6-Sabresd-Wayland/iMX6-Sabresd/build-wayland/tmp/sysroots/x86_64-nativesdk-pokysdk-linux= "
export LDFLAGS="-Wl,-O1 -Wl,--hash-style=gnu -Wl,--as-needed"
export CPPFLAGS=""
export KCFLAGS="--sysroot=$SDKTARGETSYSROOT"
export OECORE_DISTRO_VERSION="4.1.15-2.0.0"
export OECORE_SDK_VERSION="4.1.15-2.0.0"
export ARCH=arm
export CROSS_COMPILE=arm-poky-linux-gnueabi-

这个时候只要简单的对configure进行参数设置即可

./configure --prefix=/home/elfutils-0.148/build/ --host=arm-linux
make
make install

情况2:通用交叉编译工具链的编译

这种情况就是设置编译工具的路径source PATH=XXXXX,使终端能够访问到给编译工具,通用的交叉编译工具链往往没有CC,CXX,CFLAGS,LD_XXX之类的变量,这个时候就要手动加上去

先设置交叉编译工具的路径

export PATH=$PATH:/toolchain/arm-toolchain-4.9.4/bin:/toolchain/arm-toolchain-4.9.4/arm-none-linux-gnueabihf/bin:/qt4.7.4/bin

然后在设置configure参数,这里简单指定CC和CXX

./configure --prefix=/home/elfutils-0.148/build/ --host=arm-linux CC=arm-linux-cc CXX=arm-linux-g++
make
make install
 

到此处两种方法都能编译elfutils,接下来再编译ltrace,按照如上同样的方法配置ltrace,有如下报错,缺少的正式上边编译出来的头文件

configure: error: *** libelf.h or gelf.h not found on your system

如果有了头文件路径没有库路径

configure: error: *** libelf not found on your system

可用如下方法指定不在系统默认路径/use/lib下的库或头文件

CFLAGS=-I/home/elfutils-0.148/build/include LDFLAGS=-L/home/elfutils-0.148/build/lib ./configure --prefix=/home//ltrace/build/ --host=arm-linux CC=arm-linux-cc CXX=arm-linux-g++