这是我的makefile:
CC=gcc
CFLAGS=-g
LDFLAGS=-lm
EXECS= p1
all: $(EXECS)
clean:
rm -f *.o $(EXECS)
14:32:16 ****构建配置默认项目CH3-程序****
制作p1
gcc -g -ggdb -lm p1.c -o p1
/tmp/ccNTyUSA.o:在函数main':
/home/bm5788/fromVM/Workspace/CH3-Programs//p1.c:28: undefined reference to
pow'
collect2:错误:ld返回1退出状态
make:*** [p1]错误1
:目标配方' p1'失败
答案 0 :(得分:2)
此处的问题是您与数学库(-lm
选项)链接的顺序。构建时,库应该位于命令行上的源文件或目标文件之后。
因此,如果您运行命令以手动构建,它应该看起来像
gcc p1.c -o p1 -lm
问题是你的makefile并没有真正做任何事情,而且它只依赖于隐式规则。隐式规则按特定顺序使用某些变量,这些变量不会将库放在makefile中的正确位置。
尝试使用类似这样的makefile:
# The C compiler to use.
CC = gcc
# The C compiler flags to use.
# The -g flag is for adding debug information.
# The -Wall flag is to enable more warnings from the compiler
CFLAGS = -g -Wall
# The linker flags to use, none is okay.
LDFLAGS =
# The libraries to link with.
LDLIBS = -lm
# Define the name of the executable you want to build.
EXEC = p1
# List the object files needed to create the executable above.
OBJECTS = p1.o
# Since this is the first rule, it's also the default rule to make
# when no target is specified. It depends only on the executable
# being built.
all: $(EXEC)
# This rule tells make that the executable being built depends on
# certain object files. This will link using $(LDFLAGS) and $(LDLIBS).
$(EXEC): $(OBJECTS)
# No rule needed for the object files. The implicit rules used
# make together with the variable defined above will make sure
# they are built with the expected flags.
# Target to clean up. Removes the executable and object files.
# This target is not really necessary but is common, and can be
# useful if special handling is needed or there are many targets
# to clean up.
clean:
-rm -f *.o $(EXEC)
如果使用上述makefile运行make
,make
程序应首先从源文件p1.o
构建目标文件p1.c
。然后应该使用p1.o
目标文件将可执行文件p1
与标准数学库链接起来。