0

I'm using the function pow() of math.h in my project. I added the -lm flag to the Makefile but I still get the following error trying to run make:

rudp_comm.c:(.text+0x3c1): undefined reference to `pow'
rudp_comm.c:(.text+0x458): undefined reference to `pow'

How to fix this problem?

Here's the complete Makefile:

CC := gcc
CFLAGS := -Wall -Wextra -Wpedantic -O3 -lm
SRC := client/client.c client/client_func.c client/client_func.h server/server.c server/server_func.c server/server_func.h common/conf.c common/conf.h common/file_list.c common/file_list.h common/rudp_comm.c common/rudp_comm.h
OBJ := $(SRC:.c=.o)

.PHONY: all

all: client/client server/server

client/client: client/client.o client/client_func.o common/conf.o common/file_list.o common/rudp_comm.o

client/client.o: client/client.c client/client_func.h common/rudp_comm.h
client/client_func.o: client/client_func.c client/client_func.h common/conf.h common/file_list.h

server/server: server/server.o server/server_func.o common/conf.o common/file_list.o common/rudp_comm.o

server/server.o: server/server.c server/server_func.h common/rudp_comm.h
server/server_func.o: server/server_func.c server/server_func.h common/conf.h common/file_list.h

common/conf.o: common/conf.c common/conf.h
common/file_list.o: common/file_list.c common/file_list.h
common/rudp_comm.o: common/rudp_comm.c common/rudp_comm.h


clean:
        rm -f client/*.o server/*.o common/*.o core

cleanall:
        rm -f client/*.o server/*.o common/*.o core client/client server/server

In rudp_comm.c of course I included the library (#include "rudp_comm.h"), so I really can't guess what the problem could be!

Robb1
  • 4,587
  • 6
  • 31
  • 60
  • Possible duplicate of [How to fix undefined reference/unresolved external symbol error](https://stackoverflow.com/questions/12573816/how-to-fix-undefined-reference-unresolved-external-symbol-error) – Yunnosch Jan 09 '18 at 18:35

1 Answers1

2

-lm should go into LDLIBS, not CFLAGS in the Makefile.

LDLIBS = -lm

See https://www.gnu.org/software/make/manual/html_node/Implicit-Variables.html

Although, CFLAGS is also passed to the compiler, ``-lmneeds to be at the *end* of the commandline. That's why specifying it inCFLAGSdoesn't work because it needs to be passed at the *linking* stage (specifying-lm` at the end provides equivalent functionality).

P.P
  • 117,907
  • 20
  • 175
  • 238
  • Thanks, that solved my issue! I'll accept it as soon as stackoverflow will allow me to do it :) – Robb1 Jan 09 '18 at 18:48