GCC编译器有很多可选编译选项,其中-static我们比较常用,在支持动态链接的系统上,阻止连接共享库。在学习GCC选用静态库编译-static时出错显示:“/usr/bin/ld: cannot find -lc”。如果不选-static的动态库编译是正常,没有这个错误提示。
[ycxie@fedora Workspace]$ gcc hello.c -o hello [ycxie@fedora Workspace]$ gcc hello.c -o hello -static /usr/bin/ld: cannot find -lc collect2: error: ld returned 1 exit status
这问题一般是由于ld在进行库连接时找不到相应的库文件导致的。我们用dnf查一下,应该缺少什么包呢?
[ycxie@fedora Workspace]$ dnf search glibc glibc-devel.x86_64 : Object files for development using standard C libraries. glibc-devel.i686 : Object files for development using standard C libraries. glibc-utils.x86_64 : Development utilities from GNU C library glibc-static.i686 : C library static libraries for -static linking. glibc-static.x86_64 : C library static libraries for -static linking. glibc-headers.x86_64 : Header files for development using standard C libraries. glibc-headers.i686 : Header files for development using standard C libraries. glibc-nss-devel.i686 : Development files for directly linking NSS service modules glibc-nss-devel.x86_64 : Development files for directly linking NSS service modules
哥伦布发现新大陆,哈哈,这个glibc-static出现了。看说明“C library static libraries for -static linking”,这正是我们所需要的啊。
[ycxie@fedora Workspace]$ dnf list installed | grep glibc glibc.x86_64 2.25-10.fc26 @updates glibc-all-langpacks.x86_64 2.25-10.fc26 @updates glibc-common.x86_64 2.25-10.fc26 @updates glibc-devel.x86_64 2.25-10.fc26 @updates glibc-headers.x86_64 2.25-10.fc26 @updates glibc-langpack-en.x86_64 2.25-10.fc26 @updates
果然我们的电脑里没安装glibc-static,问题找到了,直接安装即可。
[ycxie@fedora Workspace]$ dnf install glibc-static
安装好了glibc-static,我们再编译一下看看:
[ycxie@fedora Workspace]$ gcc hello.c -o hello -static [ycxie@fedora Workspace]$ ./hello Hello World
到此一切正常,GCC提示“/usr/bin/ld: cannot find -lc”的错误完美解决~