路漫漫其修遠兮,吾將上下而求索

0%

/usr/bin/ld: cannot find -lxxx 的解決方法

最近被公司要求去抓 GPS晶片提供的Callback 位置,所幸官網上有提供SDK 跟文件可以研讀,目前是在 CentOS 6.6上 Compile的,詳細的kernel realease 為:2.6.32-504.1.3.el6.centos.plus.x86_64

但在 compile 時會出現以下訊息

1
2
3
4
5
[root@Demo PDS_Services]# make
bin/ exists - good.
/usr/bin/ld: cannot find -lrt
collect2: ld returned 1 exit status
make: *** [bin/pdsserviceshostx86_64] Error 1

錯誤訊息指出:少了 librt 這個library,但是查了電腦上安裝的library 之後是有安裝到的:

1
2
3
4
[root@Demo PDS_Services]# ldconfig -p | grep librt
librtmp.so.0 (libc6,x86-64) => /usr/lib64/librtmp.so.0
librt.so.1 (libc6,x86-64, OS ABI: Linux 2.6.18) => /lib64/librt.so.1
librt.so (libc6,x86-64, OS ABI: Linux 2.6.18) => /usr/lib64/librt.so

另過透過 gcc 測試的結果皆為正常:

1
2
3
4
5
6
7
8
9
10
[root@Demo PDS_Services]# gcc -lrt --verbose
Using built-in specs.
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib --with-ppl --with-cloog --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC)
COMPILER_PATH=/usr/libexec/gcc/x86_64-redhat-linux/4.4.7/:/usr/libexec/gcc/x86_64-redhat-linux/4.4.7/:/usr/libexec/gcc/x86_64-redhat-linux/:/usr/lib/gcc/x86_64-redhat-linux/4.4.7/:/usr/lib/gcc/x86_64-redhat-linux/:/usr/libexec/gcc/x86_64-redhat-linux/4.4.7/:/usr/libexec/gcc/x86_64-redhat-linux/:/usr/lib/gcc/x86_64-redhat-linux/4.4.7/:/usr/lib/gcc/x86_64-redhat-linux/
LIBRARY_PATH=/usr/lib/gcc/x86_64-redhat-linux/4.4.7/:/usr/lib/gcc/x86_64-redhat-linux/4.4.7/:/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/:/lib/../lib64/:/usr/lib/../lib64/:/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-mtune=generic'
/usr/libexec/gcc/x86_64-redhat-linux/4.4.7/collect2 --eh-frame-hdr --build-id -m elf_x86_64 --hash-style=gnu -dynamic-linker /lib64/ld-linux-x86-64.so.2 /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/crt1.o /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/crti.o /usr/lib/gcc/x86_64-redhat-linux/4.4.7/crtbegin.o -L/usr/lib/gcc/x86_64-redhat-linux/4.4.7 -L/usr/lib/gcc/x86_64-redhat-linux/4.4.7 -L/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../.. -lrtaaa -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-redhat-linux/4.4.7/crtend.o /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/crtn.o

最後在 makefile 找到某個片段:

1
2
3
4
5
ifeq ($(CPU),ppc_4xx)
CROSS_COMPILE := $(HOME)/toolchain/ELDK/usr/bin/ppc_4xx-
INCLUDE := -I.. $(QMI_INCLUDE) -I$(HOME)/toolchain/ELDK/usr/lib/gcc/powerpc-linux/4.2.2/include
LDFLAGS := -L$(HOME)/toolchain/ELDK/usr/lib -static -lrt -lpthread
endif

原來 binary會被compiled 成statically,最後重新安裝了套件就解決了:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[root@Demo PDS_Services]# yum search glibc
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirror01.idc.hinet.net
* extras: centos.cs.nctu.edu.tw
* rpmforge: mirror.oscc.org.my
* updates: ftp.jaist.ac.jp
rpmforge | 1.9 kB 00:00
updates | 3.4 kB 00:00
updates/primary_db | 749 kB 00:00
================================================== N/S Matched: glibc ==================================================
glibc-common.x86_64 : Common binaries and locale data for glibc
compat-glibc.x86_64 : Compatibility C library
compat-glibc-headers.x86_64 : Header files for development using standard C libraries.
glibc.x86_64 : The GNU libc libraries
glibc.i686 : The GNU libc libraries
glibc-devel.x86_64 : Object files for development using standard C libraries.
glibc-devel.i686 : Object files for development using standard C libraries.
glibc-headers.x86_64 : Header files for development using standard C libraries.
glibc-static.i686 : C library static libraries for -static linking.
glibc-static.x86_64 : C library static libraries for -static linking.
glibc-utils.x86_64 : Development utilities from GNU C library
kernel-headers.x86_64 : Header files for the Linux kernel for use by glibc
latrace.i686 : LD_AUDIT feature frontend for glibc 2.4+
latrace.x86_64 : LD_AUDIT feature frontend for glibc 2.4+

Name and summary matches only, use "search all" for everything.

選擇 glibc-static 安裝即可:

1
# yum install glibc-static

最後重新 make 就成功了:

1
2
3
4
5
6
7
8
9
10
11
12
[root@Demo PDS_Services]# make
bin/ exists - good.
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/libpthread.a(libpthread.o): In function `sem_open':
(.text+0x77cd): warning: the use of `mktemp' is dangerous, better use `mkstemp'
[root@Demo PDS_Services]# ll bin
total 26524
-rwxrwxr-x 1 1001 1001 7315034 May 31 14:23 pdsservicesarm
-rwxrwxr-x 1 1001 1001 4210669 May 31 14:23 pdsserviceshosti686
-rwxr-xr-x 1 root root 1753341 Jul 7 12:05 pdsserviceshostx86_64
-rwxrwxr-x 1 1001 1001 4388975 May 31 14:23 pdsservicesmips
-rwxrwxr-x 1 1001 1001 4928396 May 31 14:23 pdsservicesmipsel
-rwxrwxr-x 1 1001 1001 4550920 May 31 14:23 pdsservicesppc