Shlomi Amichay
Shlomi Amichay

Reputation: 21

main.c:(.text+0x170): undefined reference to `sqrt' (used -lm)

ok, so i'm trying to compile my code using makefile, i've got only 2 .c file and 1 .h file, i used "sqrt()" function from math.h (only in main), here is my makefile:

a.out:  GBST.o main.o
        gcc GBST.o  main.o

GBST.o: GBST.c GBST.h
        gcc -c GBST.c

main.o: main.c
        gcc -c main.c -lm

still, I get main.c:(.text+0x170): undefined reference to `sqrt' error, what can it be? (btw, i wrote -lm in GBST line before and it did not help, so i have deleted it)

Upvotes: 1

Views: 983

Answers (1)

R Sahu
R Sahu

Reputation: 206607

You need to use -lm in the link line, not in the compile line.

a.out:  GBST.o main.o
        gcc GBST.o  main.o -lm
#                          ^^^^ Need it here

GBST.o: GBST.c GBST.h
        gcc -c GBST.c

main.o: main.c
        gcc -c main.c
#                     ^^^^ Don't need it here

Upvotes: 8

Related Questions