gcc static静态编译选项提示错误:/usr/lib/ld:cannot find -lc

本文介绍了在使用GCC进行静态库编译时遇到的常见错误“/usr/lib/ld:cannotfind-lc”等问题的原因及解决办法,并对比了静态库与动态库编译的区别。

在学习gcc静态库动态库编译的时候选用静态库编译时出错显示:/usr/lib/ld:cannot find -lc

百度:/usr/lib/ld:cannot find -lc多处给的解决方案为:

然而并不能解决问题,最终定位发现是静态编译的问题。而且不止会出现这种情况:

/usr/lib/ld:cannot find -lc

/usr/lib/ld:cannot find -lgcc_s

/usr/lib/ld:cannot find -lm

等的错误,主要原因在静态编译时需要链接静调库。如上命令:

[xiaohexiansheng@centos6 app]$ gcc -static -I./libs main.c -o app -L./libs -lcrypto -lfunc

如果在编译时去掉-static选项选用动态库编译则不会出现此种情况

[xiaohexiansheng@centos6 app]$ gcc -I./libs main.c -o app -L./libs -lcrypto -lfunc
[xiaohexiansheng@centos6 app]$ ls
app  libs  main.c

非静态编译时ldd filename,显示如下,这是可执行程序所需的动态库,运行可执行程序时需要的动态库。

[xiaohexiansheng@centos6 app]$ ldd app
        linux-gate.so.1 =>  (0x004ad000)
        libcrypto.so => /usr/lib/libcrypto.so (0x03ad9000)
        libfunc.so => not found
        libc.so.6 => /lib/libc.so.6 (0x0052a000)
        libdl.so.2 => /lib/libdl.so.2 (0x0070c000)
        libz.so.1 => /lib/libz.so.1 (0x00713000)
        /lib/ld-linux.so.2 (0x00508000)

静态编译时需要将所有的.a库链接到可执行文件中,所以需要libc静态库文件,在系统找查找glibc-static提示没有库文件。

#yum install glibc-static安装静态库。

转载于:https://www.cnblogs.com/xiaohexiansheng/p/5500870.html

/opt/android-ndk-r18b/toolchains/llvm/prebuilt/linux-x86_64/bin/clang --sysroot=/opt/android-ndk-r18b/sysroot -I/include -fPIC -c -o /tmp/ffconf.3S9556Zm/test.o /tmp/ffconf.3S9556Zm/test.c /opt/android-ndk-r18b/toolchains/llvm/prebuilt/linux-x86_64/bin/clang -L/lib -L/opt/android-ndk-r18b/sysroot/usr/lib -lmpp -lunwind --sysroot=/opt/android-ndk-r18b/sysroot -o /tmp/ffconf.3S9556Zm/test /tmp/ffconf.3S9556Zm/test.o /..//bin/ld: cannot find crt1.o: No such file or directory /..//bin/ld: cannot find crti.o: No such file or directory /..//bin/ld: cannot find crtbegin.o: No such file or directory /..//bin/ld: cannot find -lmpp /..//bin/ld: cannot find -lunwind /..//bin/ld: cannot find -lgcc /..//bin/ld: cannot find -lgcc_s /..//bin/ld: cannot find -lc /..//bin/ld: cannot find -lgcc /..//bin/ld: cannot find -lgcc_s /..//bin/ld: cannot find crtend.o: No such file or directory /..//bin/ld: cannot find crtn.o: No such file or directory clang: error: linker command failed with exit code 1 (use -v to see invocation) C compiler test failed.还是出现了问题,这是命令:./configure --target-os=android --arch=aarch64 --enable-cross-compile --cross-prefix=$TOOLCHAIN/bin/$TARGET$API- --sysroot=/opt/android-ndk-r18b/sysroot --cc=$TOOLCHAIN/bin/clang --cxx=$TOOLCHAIN/bin/clang++ --enable-rkmpp --enable-libdrm --extra-cflags="-I$MPP_ROOT/include -fPIC" --enable-shared --enable-version3 --disable-static --disable-doc --extra-ldflags="-L$MPP_ROOT/lib -L/opt/android-ndk-r18b/sysroot/usr/lib -lmpp -lunwind"
03-14
[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
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值