Hilf mir, ein Makefile zu erstellen
Frage
Support haben Sie ein C-Programm von einigen Dateien enthalten, und einige wird man von einigen anderen besteht, so wie folgt:
----------------------------------------
File | Included files
----------------------------------------
main.c | stdio.h, table.h
----------------------------------------
list.c | list.h
----------------------------------------
symbol.c | symbol.h
----------------------------------------
table.c | table.h
----------------------------------------
table.h | symbol.h, list.h
----------------------------------------
Bitte helfen Sie mir eine Make-Datei zu erstellen, vielen Dank! Das ist mein Makefile, aber es gibt ein Problem? die diese Probleme debuggen können, Dank!
hello: main.o table.o
gcc main.o table.o -o hello
main.o: main.c table.h
gcc -c main.c
table.o: table.c table.h
gcc -c table.c
symbol.o: symbol.c symbol.h
gcc -c symbol.c
list.o: list.c list.h
gcc -c list.c
clean:
rm hello *.o
Lösung
Hier ist ein Anfang, nicht genau, wie ein professionelles es tun würde, aber gut für einen Anfänger:
hello: main.o list.o symbol.o table.o
gcc -o hello main.o list.o symbol.o table.o
main.o: main.c table.h symbol.h list.h
gcc -c -o main.o main.c
list.o: list.c list.h
gcc -c -o list.o list.c
symbol.o: symbol.c symbol.h
gcc -c -o symbol.o symbol.c
table.o: table.c table.h symbol.h list.h
gcc -c -o table.o table.c
clean:
rm hello *.o
Das spezifische Problem mit dem angegebenen Make-Datei ist, dass Sie nicht in allen Objektdateien sind zu verknüpfen. Da main.c
umfasst table.h
und table.h
umfasst symbol.h
und list.h
, wird Ihr Programm mit ziemlicher Sicherheit brauchen auch symbol.o
und list.o
zu verbinden.
Es ist auch gut Praxis Header Abhängigkeiten zu folgen (zB hängt main.o
auf table.h
und symbol.h/list.h
weil table.h
auf diesen beiden abhängt) - das ist, weil es keine „saubere“ Art und Weise ist eine Zwischendatei zu erhalten für Header-Abhängigkeiten.
Und Sie selten Standard-Header in Make-Dateien einfach auszudrücken, weil sie nicht erwartet -. Wenn sie es tun (neue Compiler), nur sauber und frisch machen