2016-09-21 186 views
0

这里是我的makefile:配置默认项目CH3-程序**** 化妆P1 gcc的-g -ggdb -lm p1.c的为什么我得到错误,“未定义的引用`pow'collect2:错误:ld返回1退出状态make:*** [p1]错误1”?

CC=gcc 

CFLAGS=-g 

LDFLAGS=-lm 

EXECS= p1 


all: $(EXECS) 

clean: 
    rm -f *.o $(EXECS) 

十四时32分16秒****构建-o P1 /tmp/ccNTyUSA.o:在功能main': /home/bm5788/fromVM/Workspace/CH3-Programs//p1.c:28: undefined reference to POW” collect2:错误:LD返回1个退出状态 化妆:*** [P1]错误1 :配方目标 'P1' 失败

+0

而makefile将从什么对象文件构建可执行文件'p1'?并且请发布运行'make'命令的输出(在'make clean'之后)。 –

+0

你在代码中使用pow函数吗? – Angen

+0

是的,我在我的代码中使用了pow。 –

回答

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) 

如果您使用上述生成文件运行make,该make程序应首先建立从源文件p1.c目标文件p1.o。然后应该使用p1.o目标文件将可执行文件p1与标准数学库链接在一起。

+0

是的!有效!非常感谢你。我不能够感谢你。 –

+1

@BryceMarshall你可以通过upvoting和接受他的回答更有效地回答答案:-) – njuffa

相关问题