Jarryd Goodman
Jarryd Goodman

Reputation: 487

GNU Linking and Compiling

So I'm trying to create a library of source files that must be linked to other libraries. My make file looks like

CC=gcc
CFLAGS=-Wall -g -static -std=c99 -I "./include/ImageMagick-6/"
MAGICKLIB=-Llib -lMagickWand-6.Q16 -lMagickCore-6.Q16
INCLUDE=-lfreetype -ljpeg -lfontconfig -lXext -lSM -lICE -lX11 -lXt -lbz2 -lz -lm -lpng -lgomp -lpthread -lltdl
LINK=$(INCLUDE) $(MAGICKLIB)
COMMON=setUp.o closeOut.o readImage.o writeImage.o edgeEnhance.o resizeImage.o

lib: $(COMMON)
        ar -cvq libmyip.a $(COMMON)

setUp.o: setUp.c myip.h
        $(CC) $(CFLAGS) -c setUp.c $(LINK)

closeOut.o: closeOut.c myip.h
        $(CC) $(CFLAGS) -c closeOut.c $(LINK)

readImage.o: readImage.c myip.h
        $(CC) $(CFLAGS) -c readImage.c $(LINK)

writeImage.o: writeImage.c myip.h
        $(CC) $(CFLAGS) -c writeImage.c $(LINK)

edgeEnhance.o: edgeEnhance.c myip.h
        $(CC) $(CFLAGS) -c edgeEnhance.c $(LINK)

resizeImage.o: resizeImage.c myip.h
        $(CC) $(CFLAGS) -c resizeImage.c $(LINK)

myIPTest.o: myIPTest.c myip.h
        $(CC) $(CFLAGS) -c myIPTest.c $(LINK)

myIPTest: myIPTest.o myip.h libmyip.a
        $(CC) $(CFLAGS) -o myIPTest myIPTest.o -Llib -lmyip

Running make I get no errors/warnings. I am getting unrecognized reference when I run make myIPTest. I think it might be because the .o targets (setUp.o etc) are not linking the .c files properly? I'm not sure though. I'm a noob, I've been searching the internet for hours and I can't seem to fix it. Any of you guys have any ideas?

FIXED: Removed all the $(LINK) from my .o targets and just put it at the end of myIPTest target and it worked.

Upvotes: 0

Views: 79

Answers (2)

Alexey Kukanov
Alexey Kukanov

Reputation: 12764

When you compile source files to object files with -c option, all -lsomelibrary options are useless. These options only work when the compiler links object files into an executable file.

In other words, $(LINK) should be added to the last action line in your makefile (the one which builds myIPTest); in all the other lines it's useless.

Upvotes: 2

user58697
user58697

Reputation: 7923

-Llib instructs the linker to look for libmyip.a in a lib directory. On the other hand, the lib rule creates libmyip.a in the current directory. Replace -Llib with -L. and see what happen.

PS: the rest of makefile needs some refactoring as well.

Upvotes: 0

Related Questions