Solving undefined reference library linking error in gcc

I was trying to build the first commit of git i.e commit e83c516
What I faced was a linker error as shown below

$ 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.

  • Embed Git revision in executable during remote build with NetBeans
  • How can i add an administrative build in jenkins?
  • How to organize large project in several GIT repositories?
  • Trigger hudson build when svn commit
  • Gradle: How to perform git pull through gradle?
  • How to properly use CI scripts with Git hooks to compress source
  • Git submodule from just a directory of another repository
  • Egit: Configure upstream for push and pull
  • Automatically tag with git and gradle during Android assemble
  • How do I change the Gitweb default diff format to `--color-words`?
  • How to add tags in Bitbucket? I'm using Git
  • How can I get content of a file from git index?
  • 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.

    You add -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 🙂

    Git Baby is a git and github fan, let's start git clone.