为什么 make 抱怨循环依赖?
我已经为我的项目构建了一个 make 文件,它可以工作(一切都可以编译),但它给出了这些令人恼火的错误消息:
make: Circular zpr.c <- zpr.o dependency dropped.
gcc -Wall -c -o zpr.o zpr.c
make: Circular readjpeg.c <- readjpeg.o dependency dropped.
gcc -Wall -c -o readjpeg.o readjpeg.c
make: Circular readppm.c <- readppm.o dependency dropped.
gcc -Wall -c -o readppm.o readppm.c
make: Circular SceneNode.cpp <- SceneNode.o dependency dropped.
g++ -c -o SceneNode.o SceneNode.cpp
make: Circular BoundingBoxNode.cpp <- BoundingBoxNode.o dependency dropped.
g++ -c -o BoundingBoxNode.o BoundingBoxNode.cpp
make: Circular GeometryNode.cpp <- GeometryNode.o dependency dropped.
g++ -c -o GeometryNode.o GeometryNode.cpp
make: Circular SceneGraph.cpp <- SceneGraph.o dependency dropped.
g++ -c -o SceneGraph.o SceneGraph.cpp
make: Circular testgraph.cpp <- testgraph.o dependency dropped.
g++ -c -o testgraph.o testgraph.cpp
我的 makefile 一点也不复杂,所以希望有人能够发现错误。
GXX=g++
CC=gcc
CFLAGS=-Wall
LIBS=-lGL -lglut -ljpeg
OBJS=helpers.o loadobj.o zpr.o readjpeg.o readppm.o SceneNode.o BoundingBoxNode.o GeometryNode.o SceneGraph.o testgraph.o
OBJS2=testgraph.o SceneGraph.o GeometryNode.o BoundingBox.o SceneNode.o readppm.o readjpeg.o zpr.o loadobj.o helpers.o
SRCS=testgraph.cpp SceneGraph.cpp SceneNode.cpp
.o.cpp:
$(GXX) $(CFLAGS) -c $<
.o.c:
$(CC) $(CFLAGS) -c $<
testgraph: $(OBJS)
$(GXX) $(LIBS) $(OBJS) -o testgraph
clean:
rm *.o
I have built a make file for my project, and it works (everything compiles) but it gives these irritating error messages:
make: Circular zpr.c <- zpr.o dependency dropped.
gcc -Wall -c -o zpr.o zpr.c
make: Circular readjpeg.c <- readjpeg.o dependency dropped.
gcc -Wall -c -o readjpeg.o readjpeg.c
make: Circular readppm.c <- readppm.o dependency dropped.
gcc -Wall -c -o readppm.o readppm.c
make: Circular SceneNode.cpp <- SceneNode.o dependency dropped.
g++ -c -o SceneNode.o SceneNode.cpp
make: Circular BoundingBoxNode.cpp <- BoundingBoxNode.o dependency dropped.
g++ -c -o BoundingBoxNode.o BoundingBoxNode.cpp
make: Circular GeometryNode.cpp <- GeometryNode.o dependency dropped.
g++ -c -o GeometryNode.o GeometryNode.cpp
make: Circular SceneGraph.cpp <- SceneGraph.o dependency dropped.
g++ -c -o SceneGraph.o SceneGraph.cpp
make: Circular testgraph.cpp <- testgraph.o dependency dropped.
g++ -c -o testgraph.o testgraph.cpp
My makefile is not complicated at all so hopefully someone can spot the error.
GXX=g++
CC=gcc
CFLAGS=-Wall
LIBS=-lGL -lglut -ljpeg
OBJS=helpers.o loadobj.o zpr.o readjpeg.o readppm.o SceneNode.o BoundingBoxNode.o GeometryNode.o SceneGraph.o testgraph.o
OBJS2=testgraph.o SceneGraph.o GeometryNode.o BoundingBox.o SceneNode.o readppm.o readjpeg.o zpr.o loadobj.o helpers.o
SRCS=testgraph.cpp SceneGraph.cpp SceneNode.cpp
.o.cpp:
$(GXX) $(CFLAGS) -c lt;
.o.c:
$(CC) $(CFLAGS) -c lt;
testgraph: $(OBJS)
$(GXX) $(LIBS) $(OBJS) -o testgraph
clean:
rm *.o
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的隐含规则是罪魁祸首。 它们的扩展名按照与 make 理解的顺序相反的顺序列出。
告诉 make .c 文件是从 .o 文件创建的。 由于已经有一条规则规定 .o 文件是从 .c 文件创建的,因此存在循环依赖关系,因此会出现错误。
解决方案是(或者应该是,假设合理配置的 make)很简单。
在真正常见的情况下(例如 C++ 源代码),您(通常)不需要指定自己的编译规则。 只需指定类似以下内容会更简单:
这也可能帮助您避免两个错误。
您编写的规则表示 .o 文件是从 .c 文件创建的,这是向后的。 但是正确的规则已经存在于几乎所有版本的 make 中。
您已在目标文件之前列出了库。 这在某些使用 ELF 格式对象的平台上是偶然发生的。 但这仍然是错误的。 在对象之后列出库,因为库只是为了满足未定义的外部而加载。
Your implicit rules are the culprit. They have the extensions listed in the reverse order of how they are understood by make.
tells make that .c files are created from .o files. Since there is already a rule that says that .o files are created from .c files, you have a circular dependencies and therefore the errors.
The solution is (or should be, assuming a reasonably configured make) simple.
You don't (usually) need to specify your own rules for compilation in really common cases, such as C++ sources. It would be simpler to just specify something like:
This is likely to also help you avoid two errors.
The rules you wrote say that .o files are created from .c files, which is backwards. But the correct rules already exist in nearly all versions of make.
You have listed the libraries ahead of the object files. This works by accident on some platforms that use ELF format objects. But it is still wrong. List libraries after objects because libraries are only loaded to satisfy undefined externals.