“为什么我会出错?”;对“pow'”的未定义引用;collect2:错误:ld返回了1个退出状态make:**[p1]错误1";?

“为什么我会出错?”;对“pow'”的未定义引用;collect2:错误:ld返回了1个退出状态make:**[p1]错误1";?,c,makefile,math.h,C,Makefile,Math.h,这是我的makefile: CC=gcc CFLAGS=-g LDFLAGS=-lm EXECS= p1 all: $(EXECS) clean: rm -f *.o $(EXECS) # 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 m

这是我的makefile:

CC=gcc 

CFLAGS=-g

LDFLAGS=-lm

EXECS= p1


all: $(EXECS)

clean: 
    rm -f *.o $(EXECS)
# 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)
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:对
pow'的未定义引用 collect2:错误:ld返回了1个退出状态 make:**[p1]错误1
:目标“p1”的配方失败

这里的问题是与数学库链接的顺序(使用
-lm
选项)。构建时,库应位于命令行上的源文件或对象文件之后

因此,如果您运行命令手动构建,它应该如下所示

gcc p1.c -o p1 -lm
问题是,您的makefile实际上什么都不做,它只依赖于隐式规则。隐式规则以特定顺序使用某些变量,这些变量不会将库放置在makefile中的正确位置

请尝试类似以下生成文件的操作:

CC=gcc 

CFLAGS=-g

LDFLAGS=-lm

EXECS= p1


all: $(EXECS)

clean: 
    rm -f *.o $(EXECS)
# 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.c
构建目标文件
p1.o
。然后,is应该使用对象文件
p1.o
将可执行文件
p1
与标准数学库链接在一起。

这里的问题是与数学库链接的顺序(
-lm
选项)。构建时,库应位于命令行上的源文件或对象文件之后

因此,如果您运行命令手动构建,它应该如下所示

gcc p1.c -o p1 -lm
问题是,您的makefile实际上什么都不做,它只依赖于隐式规则。隐式规则以特定顺序使用某些变量,这些变量不会将库放置在makefile中的正确位置

请尝试类似以下生成文件的操作:

CC=gcc 

CFLAGS=-g

LDFLAGS=-lm

EXECS= p1


all: $(EXECS)

clean: 
    rm -f *.o $(EXECS)
# 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.c
构建目标文件
p1.o
。然后,is应使用对象文件
p1.o
将可执行文件
p1
与标准数学库链接在一起。

并且生成文件将从哪些对象文件构建可执行文件
p1
?请发布运行
make
命令(在
makeclean
之后)的输出。您在代码中使用pow函数吗?是的,我在代码中使用了pow。我在哪里可以找到目标文件?我的意思是,您说
所有
都依赖于$(EXECS)`(即
p1
)。但是
$(EXECS)
依赖于什么?makefile将从什么对象文件构建可执行文件
p1
?请发布运行
make
命令(在
makeclean
之后)的输出。您在代码中使用pow函数吗?是的,我在代码中使用了pow。我在哪里可以找到目标文件?我的意思是,您说
所有
都依赖于$(EXECS)`(即
p1
)。但是
$(EXECS)
依赖于什么呢?是的!!成功了!非常感谢你。我真是太感谢你了。@Brycemall你可以通过投票并接受他的回答来更有效地感谢回答者:-)是的!!成功了!非常感谢你。我真是太感谢你了。@Brycemall你可以通过投票并接受他的回答来更有效地感谢回答者:-)