歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Mac OS下搭建OpenWrt編譯環境記錄

Mac OS下搭建OpenWrt編譯環境記錄

日期:2017/2/28 13:47:56   编辑:Linux教程

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的編譯環境確實是一個比較“蛋疼”的舉動

Copyright © Linux教程網 All Rights Reserved