git - 解决gcc中undefined reference library linking error

标签 git gcc build linker shared-libraries

我正在尝试构建 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 的链接有帮助。 我正在解释我在下面弄清楚的过程。

最佳答案

我读了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 的第一次提交)。

希望这有帮助:)

关于git - 解决gcc中undefined reference library linking error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27144441/

相关文章:

git - SourceTree 的自定义操作中的“找不到 Git 命令”

C重定向导致scanf不接受输入

c++ - mingw 的 gcc-5.3 下具有混合位域和枚举声明的意外 sizeof 结构

Jenkins 构建无法启动 cucumber 测试并超时

c++ - 没有 icu 的建筑 boost

git - 如何从 Visual Studio 2015 中创建 Git 裸存储库?

git - git在执行操作时会做什么:git gc-git prune

git - 如果从_any_远程 pull ,git会 pull 自动 merge 吗?

c - 有没有一种安全的方法来引用仅链接器的符号而不使用 void 表达式的地址?

build - 创建发布工件目录失败(访问路径 'C:\agent\_work\r1\a\NUL' 被拒绝。)