apr是Apache的一个开源库,支持跨平台编程。具有良好的内存管理机制,支持Linux、AIX、Windows等多种操作系统。代码是用C语言写的。最近接到一个请求,因为客户在powerpc机器上安装了Redhat,导致我们的程序无法运行,于是想到了交叉编译。而由于我们的程序使用了apr库,使用交叉编译来编译apr也就成为了必然的工作。环境设置我使用的apr版本是1.6.2,编译环境是Ubuntu18.04,GLIBC版本是2.27。通过拉起docker容器搭建编译环境。dockerrun-itd--namepower-linux--restartunless-stopped-w/var/apr-1.6.2-v`pwd`:/var/apr-1.6.2ubuntu:18.04以上命令将在host拉起一个Ubuntu18.04容器,将apr代码映射到/var/apr-1.6.2目录下。然后进入容器:dockerexec-itpower-linuxbash由于这是一个全新的Ubuntu环境,我们需要做一些基本的配置:aptupdateapt-yinstallvimapt-yinstallopenssh*编译工具链安装交叉编译为主使用gcc-powerpc64-linux-gnu是编译器,所以首先我们需要下载编译器:apt-yinstallgcc-powerpc64-linux-gnu同时,我们还需要安装一些其他的编译工具:apt-yinstalllibtoolautomakeautoconfapt-yinstallmakeapt-yinstallgcc需要说明一下,之前已经安装了gcc-powerpc64-linux-gnu,这里为什么还要安装gcc?gcc用于编译可以在当前x86环境下运行的可执行程序,后面会用到。需要注意的一件事是gcc-powerpc64-linux-gnu默认为大端编译器。如果power机器是little-endian,则需要使用little-endian编译器。小端编译器是gcc-powerpc64le-linux-gnu。编译由于apr是通过autoconf管理Makefile的,所以首先需要使用configure命令生成适用于当前平台的Makefile。命令如下:./configure--disable-dso--prefix=/usr--host=powerpc-linuxCC=/usr/bin/powerpc64-linux-gnu-gccac_cv_file__dev_zero=yesac_cv_func_setpgrp_void=yesapr_cv_process_shared_works=yesAPR_CV_MUTEX_ROBUST_SHARED=否APR_CV_TCP_NODELAY_WITH_CORK=是AC_CV_SIZEOF_SIZEOF_SIZEOF_SISEOF_SORTIC_IOVEC=16apr_cv_mutex_recursive=yes格式错误Makefile:143:目标配方'include/private/apr_escape_test_char.h'failedmake[1]:***[include/private/apr_escape_test_char.h'failedmake[1]:***[include/private/apr_escape_test_char.h]错误126make[1]:离开目录'/root/apr-1.6.2'/root/apr-1.6.2/build/apr_rules.mk:118:目标'all-recursive'failedmake的配方:***[all-recursive]错误1以上错误在执行gen_test_char时发现无法执行,因为gen_test_char是用powerpc64-linux-gun-gcc编译的,当然在当前环境下不能执行,我们手动编译用gcc只需单击一下。进入tools目录,执行:cdtoolsgccgen_test_char.c-I"(whereeverapris)/apr/include"-ogen_test_char修改完成后,回到上一级目录,继续make,还是报错:./include/apr_want.h:94:8:错误:“structiovec”的重新定义/apr.h:168,来自./include/apr_escape.h:22,来自encoding/apr_escape.c:28:/usr/powerpc64-linux-gnu/include/bits/types/struct_iovec.h:26:8:注意:最初在这里定义structiovec^~~~~/root/apr-1.6.2/build/apr_rules.mk:206:recipefortarget'encoding/apr_escape.lo'failedmake[1]:***[encoding/apr_escape.lo]Error1make[1]:Leavingdirectory'/root/apr-1.6.2'我们需要修改include/apr_want.h文件:viinclude/apr_want.h修改如下:原文内容:#ifndefAPR_IOVEC_DEFINED#defineAPR_IOVEC_DEFINEDstructiovec{void*iov_base;size_tiov_len;};#endif/*!APR_IOVEC_DEFINED*/修改为:#if0structiovec{void*iov_base;尺寸_tiov_len;};#endif/*!APR_IOVEC_DEFINED*/再次make,成功,然后执行makeinstall安装:makemakeinstall我们进入/usr/lib目录,可以看到已经生成了libapr的动态库,通过文件命令查看,可以看到确实是一个powerpc库文件:root@61ca5c46b8ef:/usr/lib#filelibapr-1.so.0.6.2libapr-1.so.0.6.2:ELF64-bitMSBsharedobject,64-bitPowerPCorcisco7500,version1(SYSV),动态链接,BuildID[sha1]=1f3dd3d15dbd80d5568544b1a81fae9d6d47cef4,withdebug_info,notstripped至此apr在powerpc64-linux上交叉编译成功
