Libevent使用Openwrt Toolchain编译结果缺少libevent_pthreads.so
2 min read

Libevent使用Openwrt Toolchain编译结果缺少libevent_pthreads.so

经查,原因是libevent在configure时找不到pthread库,而在OpenWRT的SDK下编译是可以找到的,使用-pthread参数。

$./configure -host=mipsel-openwrt-linux |grep pthread

configure: WARNING: using cross tools not prefixed with host triplet
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
checking for pthread-config... no
checking size of pthread_t... 4
config.status: creating libevent_pthreads.pc

原因如下:

$cat config.log

configure:14598: checking whether pthreads work with -pthread
configure:14681: mipsel-openwrt-linux-gcc -o conftest -g -O2 -Wall -fno-strict-aliasing -pthread conftest.c >&5
mipsel-openwrt-linux-uclibc-gcc.bin: warning: environment variable 'STAGING_DIR' not defined
conftest.c: In function 'main':
conftest.c:119:22: warning: null argument where non-null required (argument 1) [-Wnonnull]
pthread_attr_init(0); pthread_cleanup_push(0, 0);
^
conftest.c:120:22: warning: null argument where non-null required (argument 1) [-Wnonnull]
pthread_create(0,0,0,0); pthread_cleanup_pop(0);
^
conftest.c:120:22: warning: null argument where non-null required (argument 3) [-Wnonnull]
conftest.c:118:27: warning: 'th' is used uninitialized in this function [-Wuninitialized]
pthread_t th; pthread_join(th, 0);
^
mipsel-openwrt-linux-uclibc-gcc.bin: warning: environment variable 'STAGING_DIR' not defined
mipsel-openwrt-linux-uclibc-gcc.bin: warning: environment variable 'STAGING_DIR' not defined
/home/optman/openwrt/OpenWrt-Toolchain-ralink-for-mipsel_24kec+dsp-gcc-4.8-linaro_uClibc-0.9.33.2/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 /home/optman/openwrt/OpenWrt-Toolchain-ralink-for-mipsel_24kec+dsp-gcc-4.8-linaro_uClibc-0.9.33.2/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)
/home/optman/openwrt/OpenWrt-Toolchain-ralink-for-mipsel_24kec+dsp-gcc-4.8-linaro_uClibc-0.9.33.2/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'

添加-ldl标志就可以了,如下

$CFLAGS=-ldl ./configure -host=mipsel-openwrt-linux |grep pthread

configure: WARNING: using cross tools not prefixed with host triplet
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
checking size of pthread_t... 4
config.status: creating libevent_pthreads.pc

现在make就可以编译出libevent_pthreads库了

后记:
因一个应用需要libevent_pthreads.so,而PandoraBox的下载站点上没有找到,而只有libevent.so。于是使用PandoraBox提供的toolchain对libevent的源码进行编译,然后发现也没有libevent_pthreads.so,原因就是上面说的。