解决 gcc 中未定义的参考库链接错误



我试图构建 git 的第一个提交,即提交 e83c516我面临的是链接器错误,如下所示

$ make                                                                                    
gcc -g -Wall -o update-cache update-cache.o read-cache.o -lssl                                                                        
/usr/bin/ld: update-cache.o: undefined reference to symbol 'SHA1_Init@@libcrypto.so.10'                                               
/usr/bin/ld: note: 'SHA1_Init@@libcrypto.so.10' is defined in DSO /lib64/libcrypto.so.10 so try adding it to the linker command line  
/lib64/libcrypto.so.10: could not read symbols: Invalid operation                                                                     
collect2: error: ld returned 1 exit status                                                                                            
make: *** [update-cache] Error 1                                                                                                    

$ cat Makefile                                                                            
CFLAGS=-g -Wall                                                                                         CC=gcc                                                                                                                                   
PROG=update-cache show-diff init-db write-tree read-tree commit-tree cat-file                                                         
all: $(PROG)                                                                                                                                  
install: $(PROG)                                                                                                                      
        install $(PROG) $(HOME)/bin/                                                                                                                                                                                                                        
LIBS= -lssl                                                                                                                                     
init-db: init-db.o
update-cache: update-cache.o read-cache.o
        $(CC) $(CFLAGS) -o update-cache update-cache.o read-cache.o $(LIBS)
show-diff: show-diff.o read-cache.o
  $(CC) $(CFLAGS) -o show-diff show-diff.o read-cache.o $(LIBS)

所以这其中有一些链接器错误。我试图寻找它,搜索了几个地方以使用上面的错误消息找出它,但运气不佳。主要是没有太多来自堆栈溢出的链接有所帮助。我将在下面解释我为弄清楚它而采取的过程。

我读了这篇非常好的文章,解释了链接到我的库。我建议任何遇到类似问题的人先阅读它。

因此,我将帮助新用户剖析错误消息。问题是它无法找到加密库。因此,我们需要首先链接该库。

-lcrypto添加到 LIBS 库列表中。我是怎么想出来的。查看错误消息 /usr/bin/ld: update-cache.o: undefined reference to symbol 'SHA1_Init@@libcrypto.so.10' 中缺少的库。您需要从lib crypto.so.10中找出加密部分

LIBS= -lssl -lcrypto 

执行此操作后,您会收到类似的错误消息:

/usr/bin/ld: update-cache.o: undefined reference to symbol 'deflate'                                                                  
/usr/bin/ld: note: 'deflate' is defined in DSO /lib64/libz.so.1 so try adding it to the linker command line                           
/lib64/libz.so.1: could not read symbols: Invalid operation                                                                           
collect2: error: ld returned 1 exit status  

现在你知道该怎么做了。 添加-lz库。所以最后 LIBS 看起来像下面的那个

LIBS= -lssl -lcrypto -lz 

这就是解决类似链接器错误(并编译 git 的第一次提交)的方式。

希望这对:)有所帮助

最新更新