Mac OS下搭建OpenWrt编译环境记录

yxdayd 2016-10-23

Mac OS下搭建OpenWrt编译环境记录(针对官方2015.01.20 r44068 trunk)。

前言

之前已经在MacOS下搭建好了OpenWrt的编译环境,没想到更新到最新的官方Trunk之后,噩梦就此开始。现将思考过程以及应对方法做个记录。

背景知识

OpenWrt推荐用MacPorts来搭建MacOS中的相关工具。MacOS自带了gcc,版本信息如下:

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.4.0
Thread model: posix

如果安装了mp-gcc49,则会在/opt/local/bin目录下创建gcc,其版本信息如下:

gcc (MacPorts gcc49 4.9.2_1) 4.9.2
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

在搭建编译环境时,可能需要两个gcc切换(一个出错以后,切换另一个使用)。

首先,请确保安装了如下macports工具库

sudo port install coreutils e2fsprogs ossp-uuid asciidoc binutils bzip2 \
  fastjar flex getopt gtk2 intltool jikes hs-zlib openssl p5-extutils-makemaker \
  python26 subversion rsync ruby sdcc unzip gettext libxslt bison gawk \
  autoconf wget gmake ncurses findutils gnutar mpfr libmpc gcc49

说明:mpfr libmpc非必须,系统在编译gcc时,会自动从源码编译这两个库,但如果用llvm-gcc编译时,可能会出现如下错误:

checking for the correct version of the gmp/mpfr/mpc libraries... no
configure: error: Building GCC requires GMP 4.2+, MPFR 2.4.0+ and MPC 0.8.0+.
Try the --with-gmp, --with-mpfr and/or --with-mpc options to specify

问题描述

问题1:

Undefined symbols for architecture x86_64:
  "_iconv", referenced from:
      convert_using_iconv(void*, unsigned char const*, unsigned long, _cpp_strbuf*) in libcpp.a(charset.o)
     (maybe you meant: __Z14cpp_init_iconvP10cpp_reader, __cpp_destroy_iconv )
  "_iconv_close", referenced from:
      __cpp_destroy_iconv in libcpp.a(charset.o)
      __cpp_convert_input in libcpp.a(charset.o)
  "_iconv_open", referenced from:
      init_iconv_desc(cpp_reader*, char const*, char const*) in libcpp.a(charset.o)
ld: symbol(s) not found for architecture x86_64


碰到此种情况,说明你用的是mp-gcc49编译的,在这个地址有关于此错误的描述

http://stackoverflow.com/questions/12619600/libiconv-and-macos

macports的libiconv与mac系统的不一致,此时需要做如下修改:

1.切换到mac系统的gcc

2.进入staging_dir/host/usr,创建并进入lib目录,建立/opt/local/lib/中所有libiconv开头的符号链接

问题2:

Undefined symbols for architecture x86_64:
  "_ERR_remove_thread_state", referenced from:
      _rsa_sign in rsa-sign.o
ld: symbol(s) not found for architecture x86_64

这个问题最为奇怪,网上没有任何地方有对此问题的说明,经过仔细检查错误信息,发现在错误信息之前,有一个

HOSTLDFLAGS=“”

 

在mkimage的Makefile中,修改

 

#HOSTLDFLAGS="$(HOST_STATIC_LINKING)"
define Host/Compile
  -C $(HOST_BUILD_DIR) defconfig
  $(MAKE) -C $(HOST_BUILD_DIR) \
  HOSTLDFLAGS="-L/opt/local/lib" \
  tools-only
endef


实际上HOSTLDFLAGS在include/host-build.mk中定义:

 

ifneq ($(HOST_OS),Darwin)
  ifeq ($(CONFIG_BUILD_STATIC_TOOLS),y)
    HOST_STATIC_LINKING = -static
  endif
endi


如果不是Darwin(MacOS),则设置为-static,否则,否则,否则呢!?就不设置任何的值。可以修改此处为:

 

ifneq ($(HOST_OS),Darwin)
  ifeq ($(CONFIG_BUILD_STATIC_TOOLS),y)
    HOST_STATIC_LINKING = -static
  endif
else
  HOST_STATIC_LINKING = -L/opt/local/lib
endif

总结

在第二个问题的处理上纠缠了很久。看来在MacOS下搭建OpenWrt的编译环境确实是一个比较“蛋疼”的举动Mac OS下搭建OpenWrt编译环境记录

相关推荐