C语言 /usr/bin/ld: 使用 makefile 编译时找不到 -lc

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16024978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:02:52  来源:igfitidea点击:

/usr/bin/ld: cannot find -lc while compiling with makefile

cgccmakefile

提问by SealCuadrado

Context first: I have a header (event.h), a program called event.c, and the main program main.c. This program will be compiled, generating first a object program (event.o), then a static library (libevent.a) in a separate folder, and then the executable program work1.exe

上下文优先:我有一个头文件(event.h)、一个名为 event.c 的程序和主程序 main.c。这个程序将被编译,首先生成一个目标程序(event.o),然后是一个单独文件夹中的静态库(libevent.a),然后是可执行程序work1.exe

To do this I created this makefile:

为此,我创建了这个 makefile:

work1 : main.c libevent.a
    gcc -static main.c -L./lib -levent -o work1 -Wall

event.o: event.c
gcc -c event.c -Wall

libevent.a: event.o
ar rcs lib/libevento.a event.o 

clean: 
rm work1 *.o

The result of executing the makefile leads to this error:

执行makefile的结果导致这个错误:

 $ make
 gcc -c event.c -Wall
 ar rcs lib/libevent.a event.o 
 gcc -static main.c -L./lib -levent -o work1 -Wall
 /usr/bin/ld: cannot find -lc
 collect2: ld returned 1 exit status
 make: *** [work1] Error 1

Any idea what is going on here? Is there a way to compiling this without installing anything?

知道这里发生了什么吗?有没有办法在不安装任何东西的情况下编译它?

回答by hoxworth

The specific error is the following line:

具体错误如下:

/usr/bin/ld: cannot find -lc

The linker cannot find the C libraries required for statically linking your library. You can try and see if libc.aalready exists on your system by calling locate libc.a. If this returns, add an appropriate library flag pointing to the directory that includes libc.a.

链接器找不到静态链接库所需的 C 库。您可以libc.a通过调用来尝试查看您的系统中是否已经存在locate libc.a。如果返回,请添加一个适当的库标志,指向包含libc.a.

If libc.ais not installed, you unfortunately need to install the library if you want to compile your library statically. Since you stated you are on CentOS, you should be able to accomplish this with yum install glibc-static.

如果libc.a未安装,很遗憾,如果您想静态编译库,则需要安装该库。既然你说你在 CentOS 上,你应该能够用yum install glibc-static.