Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

-lm Not linking math library in makefile

Tags:

c

unix

gcc

makefile

I know this error has been beaten to death, but I cannot seem to get it to work. I have linked my makefile below:

all: gensine info cs229towav

encode.o: encode.h encode.c
    gcc -c encode.c

write.o: write.c write.h
    gcc -c write.c

gensine.o: encode.c gensine.h gensine.c helper.c write.c
    gcc -c gensine.c -lm

helper.o: helper.c helper.h
    gcc -c helper.c

read.o: read.h read.c
    gcc -c read.c

info.o:read.c info.h info.c decode.c
    gcc -c info.c

decode.o: decode.c decode.h helper.c
    gcc -c decode.c

cs229towav.o: write.c read.c cs229towav.c cs229towav.h helper.c decode.c encode.c
    gcc -c cs229towav.c -lm

gensine: encode.o gensine.o write.o helper.o
    gcc -o gensine encode.o gensine.o write.o helper.o -lm

info: read.o info.o decode.o helper.o
    gcc read.o info.o decode.o helper.o

cs229towav: write.o read.o cs229towav.o decode.o encode.o helper.o
    gcc -o write.o read.o cs229towav.o decode.o encode.o helper.o -lm

Clean:
    rm -rf *o gensine info cs229towav

When I run a command such as "make gensine" I am returned with the following result:

>cc gensine.c -o gensine
/tmp/ccojm09X.o: In function `encodeCsFormat':
gensine.c:(.text+0x4b1): undefined reference to `sin'
/tmp/ccojm09X.o: In function `encodeWavFormat':
gensine.c:(.text+0xa39): undefined reference to `sin'
collect2: error: ld returned 1 exit status

After reading this is says undefined reference to sin, which is with the math library. Those functions listed are in the "encode.c" file which are included in the "gensine.c" file.

like image 466
boddie Avatar asked Nov 10 '12 22:11

boddie


1 Answers

The command in the makefile:

gcc -o gensine encode.o gensine.o write.o helper.o -lm

does not match the command you printed at the end:

cc gensine.c -o gensine

Notice also that there is no -lm

Note that make knows how to make object files so you don't need most of the makefile. Try this (remember to indent with TABs):

.PHONY : all clean
all = gensine info
CFLAGS =-Wall
LIBS = -lm

gensine: encode.o gensine.o write.o helper.o 
       gcc -o $@ $^ $(LIBS)

info: read.o info.o decode.o helper.o
       gcc -o $@ $^ $(LIBS)

cs229towav: write.o read.o cs229towav.o decode.o encode.o helper.o
       gcc -o $@ $^ $(LIBS)

clean:
       rm -rf *.o gensine info cs229towav

Edit:

Boddie, note that your confusion arose because you thought the makefile was a script - ie. that you were running your script named make when you typed make gensine. In fact make is a command like gcc somewhere else in the filesystem (on Linux etc, type which make to see where it is). The make command expects to find an input file containing build rules called makefile or Makefile in the current directory. If it doesn't find that file it uses some built-in rules instead - hence the cc gensine.c -o gensine which is nowhere in your makefile. If you want to, you can tell make the name of the makefile (so that it doesn't use the default names) with the -f switch, as @DanielFischer described in the comments.

like image 159
William Morris Avatar answered Nov 11 '22 09:11

William Morris