0

I've done some research on this issue and have included -lm at the end of my list of tags, but I keep getting an undefined reference to ____ with several math functions like cos and sin. Edit: I included <math.h>,<stdlib.h>,<stdio.h>

CFLAGS= -std=gnu18 -Wall -Wextra -Who-unused -pedantic -g -O3 -lm
CC=gcc
PROGRAMS=main
LIBS=libct.a
OBJECTS= mex.o count.o id.o orag.o
all: $(PROGRAMS) $(LIBS)
libct.a: $(OBJECTS)
        ar rcs $@ $^
main: main.o $(OBJECTS)
%.o: %.c
     $$(CC) $(CFLAGS) -c -o $@ $<
%: %.o 
     $$(CC) $(CFLAGS) -o $@ $^
.PHONY:clean_objects clean all
clean_objects:
        rm -f *.o
clean:
        rm -f $(PROGRAMS) $(LIBS) *.o
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
s_0011
  • 1
  • 2
  • it would help if you posted the code. – ewokx Aug 05 '20 at 02:36
  • Maybe you forget to include in your source file – Sébastien Bémelmans Aug 05 '20 at 02:39
  • I included in all my files – s_0011 Aug 05 '20 at 02:41
  • 1
    Welcome to Stack Overflow. We require that a question of the form *"why doesn't this code work"* include a [minimal complete example](https://stackoverflow.com/help/minimal-reproducible-example). That's not just for our benefit; it's a vital coding skill. – Beta Aug 05 '20 at 03:07
  • Does this answer your question? [Why do you need an explicit \`-lm\` compiler option](https://stackoverflow.com/questions/10371647/why-do-you-need-an-explicit-lm-compiler-option) – phuclv Aug 05 '20 at 08:49

1 Answers1

1

Linker will not link libm unless you specify it after object files that use it. So you need to change to

LDFLAGS = -lm

and

%: %.o 
     $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
yugr
  • 19,769
  • 3
  • 51
  • 96