Un makefile è un file di testo a cui fa riferimento il comando make che descrive la generazione delle destinazioni e che contiene informazioni come dipendenze di livello origine e dipendenze relative alla generazione.
Il CDT può generare un makefile nel caso di progetti Make gestiti. Per i progetti Make standard, l'utente dovrà invece definire il makefile.
# A sample Makefile # This Makefile demonstrates and explains # Make Macros, Macro Expansions, # Rules, Targets, Dependencies, Commands, Goals # Artificial Targets, Pattern Rule, Dependency Rule. # Comments start with a # and go to the end of the line. # Here is a simple Make Macro. LINK_TARGET = test_me.exe # Here is a Make Macro that uses the backslash to extend to multiple lines. # This allows quick modification of more object files. OBJS = \ Test1.o \ Test2.o \ Main.o # Here is a Make Macro defined by two Macro Expansions. # A Macro Expansion may be treated as a textual replacement of the Make Macro. # Macro Expansions are introduced with $ and enclosed in (parentheses). REBUILDABLES = $(OBJS) $(LINK_TARGET) # Make Macros do not need to be defined before their Macro Expansions, # but they normally should be defined before they appear in any Rules. # Consequently Make Macros often appear first in a Makefile. # Here is a simple Rule (used for "cleaning" your build environment). # It has a Target named "clean" (left of the colon ":" on the first line), # no Dependencies (right of the colon), # and two Commands (indented by tabs on the lines that follow). # The space before the colon is not required but added here for clarity. clean: rm -f $(REBUILDABLES) echo Clean done # There are two standard Targets your Makefile should probably have: # "all" and "clean", because they are often command-line Goals. # Also, these are both typically Artificial Targets, because they don't typically # correspond to real files named "all" or "clean". # The rule for "all" is used to incrementally build your system. # It does this by expressing a dependency on the results of that system, # which in turn have their own rules and dependencies. all : $(LINK_TARGET) echo All done # There is no required order to the list of rules as they appear in the Makefile. # Make will build its own dependency tree and only execute each rule only once # its dependencies' rules have been executed successfully. # Here is a Rule that uses some built-in Make Macros in its command: # $@ expands to the rule's target, in this case "test_me.exe". # $^ expands to the rule's dependencies, in this case the three files # main.o, test1.o, and test2.o. $(LINK_TARGET) : $(OBJS) g++ -g -o $@ $^ # Here is a Pattern Rule, often used for compile-line. # It says how to create a file with a .o suffix, given a file with a .cpp suffix. # The rule's command uses some built-in Make Macros: # $@ for the pattern-matched target # $lt; for the pattern-matched dependency %.o : %.cpp g++ -g -o $@ -c $< # These are Dependency Rules, which are rules without any command. # Dependency Rules indicate that if any file to the right of the colon changes, # the target to the left of the colon should be considered out-of-date. # The commands for making an out-of-date target up-to-date may be found elsewhere # (in this case, by the Pattern Rule above). # Dependency Rules are often used to capture header file dependencies. Main.o : Main.h Test1.h Test2.h Test1.o : Test1.h Test2.h Test2.o : Test2.h # Alternatively to manually capturing dependencies, several automated # dependency generators exist. Here is one possibility (commented out)... # %.dep : %.cpp # g++ -M $(FLAGS) $< > $@ # include $(OBJS:.o=.dep)
D1. La vista Console riporta il messaggio di errore "Errore di avvio del generatore". Che significa?
Si è verificato un errore di avvio del generatore (make -k clean all ) (Errore di esecuzione: Avvio non riuscito)
Molto probabilmente, il comando di generazione (per impostazione predefinita "make") non si trova nel percorso. Inserirlo nel percorso e riavviare Eclipse.
È anche possibile modificare il comando di generazione con il comando che si trova nel percorso. Se si utilizzano
strumenti MinGW per la compilazione, è necessario sostituire il comando di generazione con "mingw32-make".
D2. La vista console riporta "No rule to make target 'X'".
make -k clean all make: *** No rule to make target 'clean'. make: *** No rule to make target 'all'.
Per impostazione predefinita, il programma make ricerca un file chiamato "Makefile" o "makefile". Se non riesce a trovare il file nella directory di lavoro oppure se questo file è vuoto e non contiene regole per gli obiettivi della riga comandi ("clean" e "all" in questo caso), normalmente avrà esito negativo con un messaggio di errore simile a quelli mostrati.
Se si dispone già di un Makefile valido, è possibile che sia necessario cambiare la directory di lavoro della generazione. La directory di lavoro predefinita per il comando di generazione la directory principale dei progetti. È possibile cambiare questa directory specificando una directory di generazione alternativa nelle proprietà del progetto Make. Oppure, se il Makefile ha un altro nome (ad esempio, buildFile.mk), è possibile specificare il nome impostando il comando di generazione predefinito su make -f buildFile.mk.
Se non si dispone di un Makefile valido, creare un nuovo file chiamato Makefile nella directory principale. Successivamente, è possibile aggiungere il contenuto del Makefile di esempio (sopra riportato) e modificarlo in base alle esigenze.
D3. La vista Console riporta "missing separator".
make -k clean all makefile:12: *** missing separator. Stop.
La sintassi standard dei Makefile comporta che ciascuna riga di una regola di generazione deve preceduta da un carattere di tabulazione. Spesso, questo carattere di tabulazione viene sostituito con spazi e poiché entrambi risultano con un rientro rappresentato da spazi bianchi, questo problema viene ignorato. Nell'esempio fornito, il messaggio di errore si trova alla riga del file "makefile"; per correggere il problema, inserire un carattere di tabulazione all'inizio della riga.
D4. La vista Console riporta "Target 'all' not remade because of errors".
make -k clean all make: *** [clean] Error 255 rm -f Test1.o Test2.o Main.o test_me.exe g++ -g -o Test1.o -c Test1.cpp make: *** [Test1.o] Error 255 make: *** [Test2.o] Error 255 make: *** [Main.o] Error 255 g++ -g -o Test2.o -c Test2.cpp g++ -g -o Main.o -c Main.cpp make: Target 'all' not remade because of errors.
Molto probabilmente, g++ non si trova nel percorso.
L'errore 255 viene generati da make in quanto la sua shell di comando non in grado di trovare un comando per una particolare regola.
I messaggi dal flusso di errori standard (le righe con Error 255) e il flusso di output standard (tutte le altre righe) vengono qui unite nella vista Console.
D5. Cosa segnala l'indicatore -k?
-k indica a a make di continuare a generare altre regole indipendenti anche quando una regola non riesce. Questo indicatore è molto utile per i progetti di generazione di grandi dimensioni.
È possibile rimuovere l'indicatore -k attivando Proprietà progetto > Progetto Make C/C++ > Generatore Make > Stop on first build error
D6. La vista Console contiene quanto riportato di seguito:
mingw32-make clean all process_begin: CreateProcess((null), rm -f Test1.o Test2.o Main.o test_me.exe, ...) failed. make (e=2): The system cannot find the file specified. mingw32-make: *** [clean] Error 2 rm -f Test1.o Test2.o Main.o test_me.exe
mingw32-make non è stato in grado di trovare l'utilità "rm". Sfortunatamente, MinGW non è fornito con "rm". Per correggere questo problema, sostituire la regola clean nel Makefile con:
clean: -del $(REBUILDABLES) echo Clean done
Il segno meno iniziale indica a make di considerare la regola clean come rule eseguita correttamente anche se il comandi restituisce un errore. Ciò può essere accettabile poiché il comando terminerà con un errore se i file specificati da eliminare non esistono ancora o non esistono più.