关于linux:libapr在powerpc64linux上的交叉编译

12次阅读

共计 2997 个字符,预计需要花费 8 分钟才能阅读完成。

aprapache 公司的一个反对跨平台编程的开源库,有很好的内存管理机制,反对 LinuxAIXWindows等多操作系统。代码由 C 语言编写。
近期接到一个需要,因为咱们的客户在 powerpc 机器上装置了 Redhat,导致咱们的程序无奈运行,因而想到了穿插编译。
而咱们的程序因为应用了 apr 库,所以在劫难逃地,应用穿插编译的形式编译 apr 也就成为了不可避免的工作。

环境搭建

我应用的 apr 版本为 1.6.2,编译环境为Ubuntu18.04GLIBC 版本为 2.27。通过拉起docker 容器的形式搭建编译环境。

docker run -itd --name power-linux --restart unless-stopped -w /var/apr-1.6.2 -v `pwd`:/var/apr-1.6.2 ubuntu:18.04 

以上命令会在宿主机上拉起一个 Ubuntu18.04container,并将 apr 的代码映射在 /var/apr-1.6.2 目录下。

而后进入容器:

docker exec -it power-linux bash

因为这是一个全新的 Ubuntu 环境,咱们须要做一些根底的配置:

apt update
apt -y install vim 
apt -y install openssh*

编译工具链装置

穿插编译次要应用 gcc-powerpc64-linux-gnu 这个编译器,所以首先咱们要下载该编译器:

apt -y install gcc-powerpc64-linux-gnu

同时,咱们还须要装置一些其余编译工具:

apt -y install libtool automake autoconf
apt -y install make
apt -y install gcc

须要阐明一下,后面曾经装置了 gcc-powerpc64-linux-gnu,为什么这里还要装置gccgcc 是为了编译在以后 x86 环境能够运行的可执行程序用的,在前面会用到。

有一点要留神的是,gcc-powerpc64-linux-gnu默认的是大端编译器,如果 power 机器是小端的话,须要应用小端编译器,小端编译器为gcc-powerpc64le-linux-gnu

编译

因为 apr 是通过 autoconf 的形式治理 Makefile 的,所以首先须要应用 configure 命令生成以后平台实用的Makefile,命令如下:

./configure --disable-dso --prefix=/usr --host=powerpc-linux CC=/usr/bin/powerpc64-linux-gnu-gcc  ac_cv_file__dev_zero=yes ac_cv_func_setpgrp_void=yes  apr_cv_process_shared_works=yes apr_cv_mutex_robust_shared=no apr_cv_tcp_nodelay_with_cork=yes ac_cv_sizeof_struct_iovec=16 apr_cv_mutex_recursive=yes        

执行完以上命令,Makefile曾经生成,此时,如果咱们执行make,会报错:

/bin/bash: tools/gen_test_char: cannot execute binary file: Exec format error
Makefile:143: recipe for target 'include/private/apr_escape_test_char.h' failed
make[1]: *** [include/private/apr_escape_test_char.h] Error 126
make[1]: Leaving directory '/root/apr-1.6.2'
/root/apr-1.6.2/build/apr_rules.mk:118: recipe for target 'all-recursive' failed
make: *** [all-recursive] Error 1

下面这个谬误是因为执行 gen_test_char 的时候发现执行不了造成的,因为 gen_test_char 是用 powerpc64-linux-gun-gcc 编译进去的,在以后环境当然执行不了,咱们手动用 gcc 编译一下就能够了。
进入 tools 目录,执行:

cd tools
gcc gen_test_char.c -I"(where ever apr is)/apr/include" -o gen_test_char

批改实现后,退回上一级目录,持续make,依然报错:

./include/apr_want.h:94:8: error: redefinition of 'struct iovec'
 struct iovec
        ^~~~~
In file included from /usr/powerpc64-linux-gnu/include/sys/socket.h:26:0,
                 from ./include/apr.h:168,
                 from ./include/apr_escape.h:22,
                 from encoding/apr_escape.c:28:
/usr/powerpc64-linux-gnu/include/bits/types/struct_iovec.h:26:8: note: originally defined here
 struct iovec
        ^~~~~
/root/apr-1.6.2/build/apr_rules.mk:206: recipe for target 'encoding/apr_escape.lo' failed
make[1]: *** [encoding/apr_escape.lo] Error 1
make[1]: Leaving directory '/root/apr-1.6.2'

咱们须要批改 include/apr_want.h 文件:

vi include/apr_want.h

依照上面的形式批改:

原内容:

#ifndef APR_IOVEC_DEFINED
#define APR_IOVEC_DEFINED
struct iovec
{
  void *iov_base;
  size_t iov_len;
};  
#endif /* !APR_IOVEC_DEFINED */

批改为:

#if  0
struct iovec
{
  void *iov_base;
  size_t iov_len;
};  
#endif /* !APR_IOVEC_DEFINED */

再次 make,胜利,而后执行make install 装置:

make
make install

咱们进入到 /usr/lib 目录下,能够看到 libapr 的动静库曾经生成了,通过 file 命令去查看,能够看到的确是 powerpc 的库文件:

root@61ca5c46b8ef:/usr/lib# file libapr-1.so.0.6.2
libapr-1.so.0.6.2: ELF 64-bit MSB shared object, 64-bit PowerPC or cisco 7500, version 1 (SYSV), dynamically linked, BuildID[sha1]=1f3dd3d15dbd80d5568544b1a81fae9d6d47cef4, with debug_info, not stripped

至此,aprpowerpc64-linux 的穿插编译胜利。

正文完
 0