Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is make complaining about circular dependencies?

Tags:

makefile

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 $<

.o.c:
    $(CC) $(CFLAGS) -c $<

testgraph: $(OBJS)
    $(GXX) $(LIBS) $(OBJS) -o testgraph

clean:
    rm *.o
like image 906
Hannes Ovrén Avatar asked Mar 20 '09 06:03

Hannes Ovrén


1 Answers

Your implicit rules are the culprit. They have the extensions listed in the reverse order of how they are understood by make.

.o.c:

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:

CFLAGS=-Wall
LOADLIBES=-lGL -lglut -ljpeg

OBJS=helpers.o loadobj.o zpr.o readjpeg.o readppm.o SceneNode.o \
   BoundingBoxNode.o GeometryNode.o SceneGraph.o  testgraph.o 

all: testgraph

testgraph: $(OBJS)

This is likely to also help you avoid two errors.

  1. 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.

  2. 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.

like image 94
RBerteig Avatar answered Oct 22 '22 19:10

RBerteig