如何解决对pow'未定义的引用? (已与-lm链接)?

时间:2018-01-09 18:34:46

标签: c makefile compilation

我在我的项目中使用pow()的{​​{1}}函数。 我向math.h添加了-lm标记,但我仍然在尝试运行Makefile时出现以下错误:

make

如何解决此问题?

这里是完整的rudp_comm.c:(.text+0x3c1): undefined reference to `pow' rudp_comm.c:(.text+0x458): undefined reference to `pow'

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 我包含了库(rudp_comm.c),所以我真的无法猜出问题所在!

1 个答案:

答案 0 :(得分:2)

-lm应该进入LDLIBS,而不是Makefile中的CFLAGS

LDLIBS = -lm

请参阅https://www.gnu.org/software/make/manual/html_node/Implicit-Variables.html

虽然CFLAGS也传递给了编译器,但``-lm needs to be at the *end* of the commandline. That's why specifying it in CFLAGS doesn't work because it needs to be passed at the *linking* stage (specifying - lm`在末尾提供了相同的功能。)