【问题标题】:Solving undefined reference library linking error in gcc解决 gcc 中未定义的参考库链接错误
【发布时间】:2014-11-26 08:32:33
【问题描述】:

我正在尝试构建 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)

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

【问题讨论】:

    标签: git gcc build linker shared-libraries


    【解决方案1】:

    我阅读了this really nice post 解释链接到我的库。我建议任何面临类似问题的人先阅读它。

    所以我会帮助新用户剖析错误信息。问题是它无法找到加密库。所以我们需要先链接那个库。

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

    中找出 crypto 部分
    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 的第一次提交)。

    希望这会有所帮助:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多