天天看點

交叉編譯中遇到的問題

編譯環境:

        ubuntu版本:ubuntu 14.04

        交叉編譯工具鍊:OpenWrt-Toolchain-ramips-mt7628_gcc-4.8-linaro_uClibc-0.9.33.2.Linux-i686

編譯庫:libosip2-3.5.0、libeXosip2-3.5.0

報錯内容:

1、libosip2-3.5.0

checking for the pthreads library -lpthreads... no
checking whether pthreads work without any flags... no
checking whether pthreads work with -Kthread... no
checking whether pthreads work with -kthread... no
checking for the pthreads library -llthread... no
checking whether pthreads work with -pthread... no
checking whether pthreads work with -pthreads... no
checking whether pthreads work with -mthreads... no
checking for the pthreads library -lpthread... no
checking whether pthreads work with --thread-safe... no
checking whether pthreads work with -mt... no
           

2、libeXosip2-3.5.0

libtool: link: mipsel-openwrt-linux-gcc -g -DOSIP_MT -DENABLE_DEBUG -g -DENABLE_TRACE -g -o .libs/sip_reg sip_reg.o ../src/.libs/libeXosip2.so -L/usr/local/lib /usr/local/lib/libosipparser2.so /usr/local/lib/libosip2.so -lresolv
/usr/local/lib/libosip2.so: undefined reference to `pthread_create'
/usr/local/lib/libosip2.so: undefined reference to `sem_destroy'
/usr/local/lib/libosip2.so: undefined reference to `sem_wait'
/usr/local/lib/libosip2.so: undefined reference to `sem_post'
/usr/local/lib/libosip2.so: undefined reference to `sem_init'
/usr/local/lib/libosip2.so: undefined reference to `sem_trywait'
/usr/local/lib/libosip2.so: undefined reference to `pthread_join'
collect2: error: ld returned 1 exit status
           

解決方法:在執行./configure檔案後加入參數 LDFLAGS='-ldl'

執行腳本:

1、libosip2-3.5.0

./configure --host=mipsel-openwrt-linux LDFLAGS='-ldl'
make clean
make
make install
           

2、libeXosip2-3.5.0

./configure --host=mipsel-openwrt-linux PKG_CONFIG_PATH=/usr/local/lib/pkgconfig LDFLAGS='-ldl'
make clean
make
make install
           

---------------------------------------------------------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------------------------------------------------------

解決過程:

首先,我執行./configure -h檢視幫助資訊,發現

--enable-pthread enable support for POSIX threads. (default=autodetect)
           

是以在配置腳本中加入這個選項

./configure --host=mipsel-openwrt-linux --enable-pthread
make clean
make
make install
           

libosip2-3.5.0庫可以成功配置、編譯、安裝,但是但是但是,這是錯的

因為接下來就是要交叉編譯 libeXosip2-3.5.0 這個庫,而上面的庫是這個庫的依賴庫,編譯過程中會出現上面報錯内容中libeXosip2-3.5.0 的錯。

是以直接加 --enable-pthread 并不能解決問題,問題還是在于沒有正确編譯 libosip2-3.5.0 庫,然後我對比沒有使用工具鍊和使用工具鍊間的差異,發現交叉編譯配置後生成的config.h檔案中沒有定義HAVE_PTHREAD宏,于是手動增加

#define HAVE_PTHREAD 1
           

這一語句,再次編譯安裝發現仍然不能解決問題,再接着就是分析兩種配置的配置資訊,沒有使用工具鍊的配置資訊如下

checking for the pthreads library -lpthreads... no
checking whether pthreads work without any flags... no
checking whether pthreads work with -Kthread... no
checking whether pthreads work with -kthread... no
checking for the pthreads library -llthread... no
checking whether pthreads work with -pthread... yes
checking for joinable pthread attribute... PTHREAD_CREATE_JOINABLE
checking if more special flags are required for pthreads... no
           

對比後可以得到前面是一緻的,隻有後面幾行有差異,是以這次我把兩次編譯的config.log檔案複制出來,在列印那幾句不同調試資訊的位置進行查找,最終發現了幾行關鍵的調試資訊

/opt/OpenWrt-Toolchain-ramips-mt7628_gcc-4.8-linaro_uClibc-0.9.33.2.Linux-i686/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2/bin/../lib/gcc/mipsel-openwrt-linux-uclibc/4.8.3/../../../../mipsel-openwrt-linux-uclibc/bin/ld: warning: libdl.so.0, needed by /opt/OpenWrt-Toolchain-ramips-mt7628_gcc-4.8-linaro_uClibc-0.9.33.2.Linux-i686/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2/bin/../lib/gcc/mipsel-openwrt-linux-uclibc/4.8.3/../../../../mipsel-openwrt-linux-uclibc/lib/libpthread.so.0, not found (try using -rpath or -rpath-link)
/opt/OpenWrt-Toolchain-ramips-mt7628_gcc-4.8-linaro_uClibc-0.9.33.2.Linux-i686/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2/bin/../lib/gcc/mipsel-openwrt-linux-uclibc/4.8.3/../../../../mipsel-openwrt-linux-uclibc/lib/libpthread.so.0: undefined reference to `dlclose'
/opt/OpenWrt-Toolchain-ramips-mt7628_gcc-4.8-linaro_uClibc-0.9.33.2.Linux-i686/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2/bin/../lib/gcc/mipsel-openwrt-linux-uclibc/4.8.3/../../../../mipsel-openwrt-linux-uclibc/lib/libpthread.so.0: undefined reference to `dlopen'
/opt/OpenWrt-Toolchain-ramips-mt7628_gcc-4.8-linaro_uClibc-0.9.33.2.Linux-i686/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2/bin/../lib/gcc/mipsel-openwrt-linux-uclibc/4.8.3/../../../../mipsel-openwrt-linux-uclibc/lib/libpthread.so.0: undefined reference to `dlsym'
           

然後百度一搜,結果是缺少了LDFLAGS='-ldl' 選項,最後在兩個庫的配置編譯腳本中都加上這個選項後,編譯安裝完美通過。