交叉编译Hi3536上面使用的nginx

https://www.jianshu.com/p/5d9b60f7b262

宿主机操作系统为ubuntu 16.04。交叉编译需要用到的软件包为
openssl-OpenSSL_1_1_0i.tar.gz
pcre-8.42.tar.gz
nginx-1.14.0.tar.gz
暂时还没有把zlib加上

一、注意openssl和pcre只需要把源码解压缩,不需要单独交叉编译!
开始的时候不知道,傻傻的把这两个库弄半天把它们编译过去了。谁知道nginx的--with-pcre和--with-openssl选项,指定的是这两个库源代码的路径,并非安装路径!nginx的编译系统只会从/usr、/usr/local等少数几个目录查找是否有预编译的pcre、zlib、openssl等库。对于交叉编译,直接把交叉编译后的pcre等安装在/usr、/usr/local显然不合适,因此需要使用--with-pcre和--with-openssl指定源代码的位置

二、交叉编译nginx
在x64 linux上面编译非常简单的nginx,没想到在交叉编译的时候巨多坑。下面一个configure是编译成功的配置

./configure --with-http_ssl_module --with-cc=arm-hisiv400-linux-gcc --with-cpp=arm-hisiv400-linux-cpp --with-pcre=/home/src/pcre-8.42 --with-openssl=/home/src/openssl-OpenSSL_1_1_0i --without-http_gzip_module --without-http_upstream_zone_module

踩坑一,configure的时候报错

checking for C compiler ... found but is not working
./configure: error: C compiler arm-hisiv400-linux-gcc is not found

这是因为nginx编译的时候除了检查cc是否存在,还需要执行编译后的程序。很明显交叉编译的程序无法执行。解决办法

vi auto/cc/name

    ngx_feature="C compiler"
    ngx_feature_name=
    ngx_feature_run=yes
   ==> ngx_feature_run=no

踩坑二,configure的时候报错

checking for int size ...objs/autotest: 1: objs/autotest: Syntax error: word unexpected (expecting ")")
bytes
./configure: error: can not detect int size

同样也是因为cc编译后的程序无法本地执行导致。解决办法

vi auto/types/sizeof

ngx_test="$CC $CC_TEST_FLAGS $CC_AUX_FLAGS \
   ==> ngx_test="gcc $CC_TEST_FLAGS $CC_AUX_FLAG \

if [ -x $NGX_AUTOTEST ]; then
    ngx_size=`$NGX_AUTOTEST`
   ==>    ngx_size=4

踩坑三,make的时候报错

cd /home/src/pcre-8.42
&& if [ -f Makefile ]; then make distclean; fi
&& CC="arm-hisiv400-linux-gcc" CFLAGS="--host=arm-hisiv400-linux -pipe"
./configure --disable-shared
...
configure: error: in `/home/src/pcre-8.42':
configure: error: C compiler cannot create executables

这是因为编译pcre的时候,nginx没有正确设置交叉编译链。解决

vi auto/options

PCRE_CONF_OPT=
   ==> PCRE_CONF_OPT=--host=arm-hisiv400-linux

踩坑四,make的时候报错

src/core/ngx_rwlock.c:125:2: error: #error ngx_atomic_cmp_set() is not defined!

报错的地方,nginx的源码是这样的

#if (NGX_HTTP_UPSTREAM_ZONE || NGX_STREAM_UPSTREAM_ZONE)

#error ngx_atomic_cmp_set() is not defined!

#endif

解决办法,在configure的时候加上--without-http_upstream_zone_module

踩坑五,make的时候报错

src/os/unix/ngx_errno.c: In function ‘ngx_strerror’:
src/os/unix/ngx_errno.c:37:31: error: ‘NGX_SYS_NERR’ undeclared (first use in this function)
msg = ((ngx_uint_t) err < NGX_SYS_NERR) ? &ngx_sys_errlist[err]:

出错的原因仍然是因为交叉编译程序无法本地运行,导致NGX_SYS_NERR宏没有赋值。解决办法,手动编辑 objs/ngx_auto_config.h,加上

#ifndef NGX_SYS_NERR
#define NGX_SYS_NERR  132
#endif

踩坑六,make的时候报错

/home/src/openssl-OpenSSL_1_1_0i/.openssl/lib/libssl.a: error adding symbols: File format not recognized
collect2: error: ld returned 1 exit status
objs/Makefile:236: recipe for target 'objs/nginx' failed

这是SSL编译的时候调用了x86的gcc,而非交叉编译gcc导致的。解决办法
vi auto/lib/openssl/make

&& ./config --prefix=$ngx_prefix no-shared no-threads $OPENSSL_OPT \
改为
&& ./Configure --prefix=$ngx_prefix no-shared no-threads --cross-compile-prefix=arm-hisiv400-linux- linux-generic32 \

踩坑七,make的时候报错

objs/src/core/ngx_cycle.o: In function 'ngx_init_cycle':
/home/src/nginx-1.14.0/src/core/ngx_cycle.c:476: undefined reference to 'ngx_shm_alloc'
/home/src/nginx-1.14.0/src/core/ngx_cycle.c:685: undefined reference to 'ngx_shm_free'

解决办法
vi objs/ngx_auto_config.h

#ifndef NGX_HAVE_SYSVSHM
#define NGX_HAVE_SYSVSHM 1
#endif



作者:叶迎宪
链接:https://www.jianshu.com/p/5d9b60f7b262
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值