2015-05-03 23 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