2015-05-03 48 views
-1

我有5個文件,我必須使用makefile來創建一個文件。如何創建一個合適的Makefile,並確定依賴關係的順序?

student.c has #include "student.h" 

linkedlist.c has #include "linkedlist.h" 

and main has #include "linkedlist.h" and #include "student.h" 

student.c 
student.h 
linkedlist.c 
linkedlist.h 
main.c 

我不知道該命令是否重要,以解決依賴關係。 我想我真正要問的是底層依賴意味着什麼? 有人可以澄清如何正確使用makefile爲未來的項目?

+0

此問題與您的問題類似。答案要複雜得多:https://stackoverflow.com/questions/1838040/how-exactly-do-i-use-a-makefile?rq=1 –

回答

1

基本上,你需要知道的是:

#You can make some const variables like `CC = gcc` 
#and use them in the Makefile like that `$(CC)` 
#(you basically wrap the variable with brackets and 
#put a dollar sign before it). 

CC = gcc 

#The order is crutial in a makefile. 

# First of all you want to compile `main` using objects 
# which are yet to be linked. 
main: student.o linkedlist.o 
    # Line below is a command 
    # that is already well known to you I guess: "gcc -o main ..." 
    $(CC) -o main student.o linkedlist.o 

# If makefile doesn't have the newest version of a .o file, 
# then it goes to lines below 
# to find out, how can it obtain the newest version 
# of student.o or linkedlist.o 

# This is how it can produce the .o file: 
student.o: student.c 
    $(CC) -c student.c 
    # -c flag is crutial here, because it means that you want to create 
    # a .o (object) file. Not an executable program. 

# Same with linkedlist.c: 
linkedlist.o: linkedlist.c 
    $(CC) -c linkedlist.c 

我希望它的作品,我沒有測試它。如果我犯了任何錯誤,請糾正我。


一兩件事:請記住,你必須使用TABS而不是縮進的空格線。

+1

「你必須使用TABS而不是SPACES來縮進行「---製造商搶先解決了製表符與空格問題。 – Daniel