CentOS7 编译C++遇到/usr/bin/ld: cannot find -l***

本文解决 CentOS7 下使用 C++ 编译时遇到的无法找到链接库问题,如 tcmalloc, lz, snappy 和 bz2 等。提供了检查库安装状态的方法及如何指定库路径的三种方案。

centos7编译C++遇到如下问题:

/usr/bin/ld: cannot find -ltcmalloc

/usr/bin/ld: cannot find -lz

/usr/bin/ld: cannot find -lsnappy

/usr/bin/ld: cannot find -lbz2

等问题:


这是因为编译器找不到相应的库

解决方案:


1. 安装了,但是未指定链接库的路径。

如查找snappy库,查看在/usr/lib中有无对应的 *snappy*.so。如果没有,可以通过find / -name *snappy*来查找,(一般会在/usr/lib64中有*snappy*.so.1(数字不固定))。

找到后可以通过: 

方式一:将其cp到/usr/lib中,改名为*snappy*.so

方式二:export LD_LIBBRARY_PATH="/usr/lib64"

方式三:直接改/etc/ld.so.conf文件,在文件中添加/usr/lib64(目录为你的库所在目录),然后执行ldconfig

2.如果查找后,均未找到相应的库,那么需要安装,如lz,则通过yum install zlib-devel来安装。


[root@localhost build]# echo $LD_LIBRARY_PATH /usr/local/smartchip/lib:/usr/local/openmpi/lib:/usr/local/openmpi/lib: [root@localhost build]# export LD_LIBRARY_PATH=/path/to/library:$LD_LIBRARY_PATH [root@localhost build]# find / -name "crt1.o" 2>/dev/null /usr/lib64/crt1.o [root@localhost build]# ln -s /usr/lib64/crt1.o /usr/lib/crt1.o [root@localhost build]# yum install -y gcc Last metadata expiration check: 1:58:52 ago on Wed 19 Nov 2025 02:35:42 PM CST. Package gcc-7.3.0-2020033101.58.p01.ky10.x86_64 is already installed. Dependencies resolved. Nothing to do. Complete! [root@localhost build]# ../configure --prefix=/usr/local/gcc-10.2.0 checking build system type... x86_64-pc-linux-gnu checking host system type... x86_64-pc-linux-gnu checking target system type... x86_64-pc-linux-gnu checking for a BSD-compatible install... /usr/bin/install -c checking whether ln works... yes checking whether ln -s works... yes checking for a sed that does not truncate output... /usr/bin/sed checking for gawk... gawk checking for libatomic support... yes checking for libitm support... yes checking for libsanitizer support... yes checking for libvtv support... yes checking for libhsail-rt support... yes checking for libphobos support... yes checking for gcc... gcc checking whether the C compiler works... yes checking for C compiler default output file name... a.out checking for suffix of executables... checking whether we are cross compiling... no checking for suffix of object files... o checking whether we are using the GNU C compiler... yes checking whether gcc accepts -g... yes checking for gcc option to accept ISO C89... none needed checking for g++... g++ checking whether we are using the GNU C++ compiler... yes checking whether g++ accepts -g... yes checking whether g++ accepts -static-libstdc++ -static-libgcc... no checking for gnatbind... no checking for gnatmake... no checking whether compiler driver understands Ada... no checking how to compare bootstrapped objects... cmp --ignore-initial=16 $$f1 $$f2 checking for objdir... .libs configure: WARNING: using in-tree isl, disabling version check The following languages will be built: c,c++,fortran,lto,objc *** This configuration is not supported in the following subdirectories: gnattools gotools target-libada target-libhsail-rt target-libphobos target-zlib target-libgo target-libffi target-liboffloadmic (Any other directories should still work fine.) checking for default BUILD_CONFIG... bootstrap-debug checking for --enable-vtable-verify... no /usr/bin/ld: cannot find crti.o: No such file or directory /usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/7.3.0/libgcc.a when searching for -lgcc /usr/bin/ld: cannot find -lgcc /usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/7.3.0/libgcc_s.so when searching for -lgcc_s /usr/bin/ld: cannot find -lgcc_s /usr/bin/ld: cannot find -lc /usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/7.3.0/libgcc.a when searching for -lgcc /usr/bin/ld: cannot find -lgcc /usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/7.3.0/libgcc_s.so when searching for -lgcc_s /usr/bin/ld: cannot find -lgcc_s /usr/bin/ld: cannot find crtn.o: No such file or directory collect2: error: ld returned 1 exit status configure: error: I suspect your system does not have 32-bit development libraries (libc and headers). If you have them, rerun configure with --enable-multilib. If you do not have them, and want to build a 64-bit-only compiler, rerun configure with --disable-multilib.
11-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值