2017-07-13 1 views
-1

Ich versuche, ein Makefile zu schreiben, das mehrere Dateien kompiliert. Hier ist mein Versuch:C Makefile mit mehreren Dateien

CC = gcc 
CFLAGS = -ansi -Wall -g -O0 -Wwrite-strings -Wshadow \ 
    -pedantic-errors -fstack-protector-all 
PROGS = myprog.o test1 test2 test3 test4 \ 

all: $(PROGS) 

clean: 
     rm -f *.o $(PROGS) *.tmp 

$(PROGS): myprog.o 

myprog.o: prog-implementation.h myprog.c myprog.h 
    gcc $(CFLAGS) -c myprog.c 
memory_checker: memory_checker.o 
    gcc -o memory_checker memory_checker.o 
memory_checker.o: memory_checker.c memory_checker.h 
    gcc $(CFLAGS) -c memory_checker.c 
test1: test1.o 
    gcc -o test1 test1.o myprog.o 
test1.o: test1.c myprog.h myprog.c 
    gcc $(CFLAGS) -c test1.c myprog.c 
test2: test2.o 
    gcc -o test2 test2.o myprog.o 
test2.o: test2.c myprog.h 
    gcc $(CFLAGS) -c test2.c 
test3: test3.o 
    gcc -o test3 test3.o myprog.o 
test3.o: test3.c myprog.h 
    gcc $(CFLAGS) -c test3.c 
test4: test4.o 
    gcc -o test4 test4.o myprog.o memory_checker.c 
test4.o: test4.c myprog.h memory_checker.h 
    gcc $(CFLAGS) -c test4.c 

Wie Sie wahrscheinlich von der Make-Datei kann sagen, der Hauptcode ist in myprog.c, die myprog.h und Prog-implementation.h enthält. Ich habe auch 4 Tests für meinen Code, von denen der letzte einen memory_checker hat, um sicherzustellen, dass ich den gesamten dynamisch zugewiesenen Speicher freigegeben habe. Bei den Tests handelt es sich um die main() -Methoden, die * .c-Dateien sind und sind.

Jedes Mal, wenn ich versuchen, die Make-Datei zu verwenden, ist es mir den Fehler

make: Circular myprog.o <- myprog.o dependency dropped. 
make: Nothing to be done for `all'. 

Alle meine Idents sind mit Tabs und keine Leerzeichen gibt, so dass nicht das Problem ist. Ich bin auch ziemlich sicher, dass es keine größeren Probleme mit meinem Code gibt, also muss das Makefile mein Problem sein. Jede Hilfe würde sehr geschätzt werden.

+0

Haben Sie versucht, nach dem Fehler zu suchen? Ich habe [diesen Beitrag] gefunden (https://stackoverflow.com/questions/21324697/makefile-circular-dependency-droped). Das klingt interessant! –

+0

Es sieht so aus, als ob Sie '$ (PROGS)' definiert haben, um von 'myprog.o' abhängig zu sein, aber' PROGS' hat auch diese Abhängigkeit. Einfach raten. Das ist viel zu komplex. make kann mit Hilfe von Platzhaltern alle Objektdateien für alle C-Dateien definieren. Suchen Sie einfach nach einem Tutorial. – jiveturkey

+0

Wenn nicht zu Lernzwecken, verwenden Sie Makefiles nicht manuell. Verwenden Sie ein modernes Werkzeug wie CMake. –

Antwort

1

Diese Fehlermeldung ziemlich klar sein sollte, :

make: Circular myprog.o <- myprog.o dependency dropped. 

Er sagt, dass myprog.o auf myprog.o abhängt. Das heißt, um myprog.o zu machen, muss es zuerst myprog.o machen, was ein logischer Fehler ist. Der Grund dafür ist dieser Teil in Ihrem Makefile:

PROGS = myprog.o test1 test2 test3 test4 \ 

....  

$(PROGS): myprog.o 

Die zweite Zeile sagt, dass alle PROGS auf myprog.o abhängen. Einer der PROGS ist myprog.o, so dass es jetzt auf sich selbst ankommt.

Um zu beheben, entfernen Sie myprog.o aus der Liste PROGS.

+0

Danke! Das hat es behoben –

0

Dies viele Fehler und unnötige Komplexität hat, also werde ich Ihnen nur eine bessere Lösung mit einigen Kommentaren präsentieren (es Sie verwenden übernimmt GNU machen):

CC := gcc 
CFLAGS := -ansi -Wall -g -O0 -Wwrite-strings -Wshadow \ 
    -pedantic-errors -fstack-protector-all 

PROGS := test1 test2 test3 test4 

# check these lists of object files needed for each binary, I extracted 
# it from your rules in the question: 
test1_OBJS := test1.o myprog.o 
test2_OBJS := test2.o 
test3_OBJS := test3.o myprog.o 
test4_OBJS := test4.o myprog.o memory_checker.o 
OBJS := $(sort $(test1_OBJS) $(test2_OBJS) $(test3_OBJS) $(test4_OBJS)) 

all: $(PROGS) 

clean: 
    rm -f *.o *.d $(PROGS) *.tmp 

# the following rules link your final targets from the object files 
# specified in the variables above: 

test1: $(test1_OBJS) 
    $(CC) [email protected] $^ 

test2: $(test2_OBJS) 
    $(CC) [email protected] $^ 

test3: $(test3_OBJS) 
    $(CC) [email protected] $^ 

test4: $(test4_OBJS) 
    $(CC) [email protected] $^ 

# this automatically generates dependency files (with your headers) 
%.d: %.c 
    $(CC) -MM -MT"[email protected] $(@:.d=.o)" [email protected] $(CFLAGS) $< 

# and this includes them 
ifneq ($(MAKECMDGOALS),clean) 
-include $(OBJS:.o=.d) 
endif 

# finally, all you need is a pattern rule compiling your object files: 

%.o: %.c 
    $(CC) -c [email protected] $(CFLAGS) $< 

.PHONY: all clean 
+0

Ich habe versucht, Ihre Makefile, die Sie vorgeschlagen, aber immer noch die "nichts zu tun für alle" Fehler –

+0

Fehler, aber Sie haben Ihre Quelldateien ('test1.c',' myprog.c', etc) in der aktuelles Verzeichnis, nicht wahr? –

+0

Ja, ich habe alle Dateien im aktuellen Verzeichnis –

Verwandte Themen