Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 995
Shubham Chaudhary Avatar asked Nov 26 '14 08:11

Shubham Chaudhary


1 Answers

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 :)

like image 141
Shubham Chaudhary Avatar answered Sep 18 '22 12:09

Shubham Chaudhary