Examples of learning and implementing C language makefile

  • 2020-05-17 05:59:07
  • OfStack

Learning and implementing examples of C language makefile

As the saying goes, programmers who can't write makefile are not good programmers.

After watching the makefile tutorial written by many people, I feel it is too difficult to understand. It is not as easy to understand as what teacher wei dongshan said in the video.

So let's write down these symbols so we don't forget what they are.

Here's an example:


<span style="font-size:18px;">Person: main.o person.o   
  g++ -o $@ $^      
%.o : %.cpp 
  g++ -c -o $@ $<  
clean: 
  rm -rf *.o Person</span> 

Among them:

1. Person represents the target file to be generated.

main.o person.o is a dependent file

So Person: main.o person.o

This means that to generate Person, you need to rely on main.o and person.o.

2. $@ : represents the target file, and the target file here is Person
$^ : represents all the dependent files following the target file, i.e..o
$ < : refers to the first dependency file, and the first one here refers to main.o

3, %.o: %.cpp

%,o: %,cpp preceded by % is a wildcard that matches all.o files and all.cpp files

4. Another point to note is that the g++ above is not a space, but an tab. In general, tab is the size of four Spaces.

5, clean:

If you do make clean,makefile will clear all the.o and Person target files.

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: