1 问题描述
在编译curl相关的应用的时候,GCC报错,提示找不到libpthread.so.0
mips-linux-gnu-gcc curl_ftp.c -L../../../buildroot/output/staging/usr/lib -lpthread -lz -lcurl -lcrypto -lssl -Wall -I/home/hhp/x2000/x2000_v5.0/buildroot/output/staging/usr/include -O2 -o test
/home/hhp/x2000/x2000_v5.0/prebuilts/toolchains/mips-gcc720-glibc226/bin/../lib/gcc/mips-linux-gnu/7.2.0/../../../../mips-linux-gnu/bin/ld: cannot find /lib/libpthread.so.0
/home/hhp/x2000/x2000_v5.0/prebuilts/toolchains/mips-gcc720-glibc226/bin/../lib/gcc/mips-linux-gnu/7.2.0/../../../../mips-linux-gnu/bin/ld: cannot find /usr/lib/libpthread_nonshared.a
collect2: error: ld returned 1 exit status
Makefile:12: recipe for target 'test' failed
make: *** [test] Error 1
2 原因
因为curl_ftp.c应用需要libcurl.so,我的curl是通过buildroot编译出来的,所以通过 -L../../../buildroot/output/staging/usr/lib指定一下libcurl.so所在路径,但是只要指定这个路径就会报错,找不到libpthread.so.0,
进入到../../../buildroot/output/staging/usr/lib目录发现也存在libpthread.so.0,因为-L指定了路径,编译器直接使用了../../../buildroot/output/staging/usr/lib/libpthread.so.0,而这个so是一个错误的动态库,只是一个简单的文本。
hhp@ubuntu:~/x2000/x2000_v5.0/buildroot/output/staging/usr/lib$ ls -l libpthread*
-rw-r--r-- 1 hhp hhp 387982 1月 15 09:17 libpthread.a
-rw-r--r-- 1 hhp hhp 1676 1月 15 09:17 libpthread_nonshared.a
-rw-r--r-- 1 hhp hhp 226 1月 15 09:17 libpthread.so
hhp@ubuntu:~/x2000/x2000_v5.0/buildroot/output/staging/usr/lib$ cat libpthread.so
/* GNU ld script
Use the shared library, but some functions are only in
the static library, so try that secondarily. */
OUTPUT_FORMAT(elf32-tradlittlemips)
GROUP ( /lib/libpthread.so.0 /usr/lib/libpthread_nonshared.a )
hhp@ubuntu:~/x2000/x2000_v5.0/buildroot/output/staging/usr/lib$
hhp@ubuntu:~/x2000/x2000_v5.0/buildroot/output/staging/usr/lib$
hhp@ubuntu:~/x2000/x2000_v5.0/buildroot/output/staging/usr/lib$
hhp@ubuntu:~/x2000/x2000_v5.0/buildroot/output/staging/usr/lib$
3 解决办法
使用-L../../../buildroot/output/target/usr/lib下面的库进行编译,这个路径下的库也是buildroot生成的,区别是都是动态库并且没有其他乱七八糟的文件
mips-linux-gnu-gcc curl_ftp.c -L../../../buildroot/output/target/usr/lib -lpthread -lz -lcurl -lcrypto -lssl -Wall -I/home/hhp/x2000/x2000_v5.0/buildroot/output/staging/usr/include -O2 -o test