这里的问题是您与数学库链接的顺序(-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)
如果您使用上述生成文件运行make
,该make
程序应首先建立从源文件p1.c
目标文件p1.o
。然后应该使用p1.o
目标文件将可执行文件p1
与标准数学库链接在一起。
而makefile将从什么对象文件构建可执行文件'p1'?并且请发布运行'make'命令的输出(在'make clean'之后)。 –
你在代码中使用pow函数吗? – Angen
是的,我在我的代码中使用了pow。 –