Robb1
Robb1

Reputation: 5025

How to solve 'undefined reference to pow' (already linked with -lm)?

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!

Upvotes: 0

Views: 219

Answers (1)

P.P
P.P

Reputation: 121397

-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).

Upvotes: 2

Related Questions