Solving undefined reference library linking error in gcc
$ 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)
So there are some linker errors in this. I tried to look for it, searched several places to figure it out using the error message above with little luck. Mainly there was not much links from stackoverflow that helped.
I’m explaining the process I took to figure it out below.
- Keeping modified library files updated in git
- Managing Internal Shared Libraries with Git and Nuget
- Sharing common code including Plugins
- Sharing custom code between two NodeJS microservices
- What is the best way to make shared libraries available to multiple applications?
One Solution collect form web for “Solving undefined reference library linking error in gcc”
I read this really nice post explaining library linking to me. I’d suggest anyone facing similar problem to read it first.
So I’ll help new user to dissect the error message. The problem is that it is not able to find the crypto library. So we’ll need to link that library first.
-lcrypto to the LIBS library list. How did I figure that out. Look at the missing library in error message
/usr/bin/ld: update-cache.o: undefined reference to symbol 'SHA1_Init@@libcrypto.so.10'. You need to figure out the crypto part from libcrypto.so.10
LIBS= -lssl -lcrypto
Once you do that you get a similar error message:
/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
Now you know what to do. Add
-lz library. So finally LIBS looks like the one below
LIBS= -lssl -lcrypto -lz
That’s how you solve similar linker errors (and compile the first commit of git).
Hope this helps 🙂