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