Linux project automation build tool - make/Makefile

1. Background

    · Whether you can write Makefile, it shows from the side whether a person has the ability to complete large-scale projects.

   There are countless source files in a project, which are placed in several directories according to type, function and module. Makefile defines a series of rules to specify which files need to be compiled first, which files need to be compiled later, and which files Requires recompilation and even more complex functional manipulations.

    · The benefit of Makefile is - "automatic compilation", once it is written, only one make command is needed, and the entire project is completely automatically compiled, which greatly improves the efficiency of software development.

    · make is a command tool, a command tool that interprets the instructions in the Makefile.

   · make is a command, Makefile is a file, the two are used together to complete the automatic construction of the project.

Example: hello.c

    #include <stdio.h>
    
    intmain()
    {
        printf("hello Makefile!\n");
        return 0;
    }



2. Principle

    How make works, in the default way, that is, just enter the make command, then:

1) make will look for a file named Makefile in the current directory

2) If found, it will find the first target file in the file, in the above example, it will find the hello file and use it as the final target file

3) If the hello file does not exist, or the modification time of the hello.o file that hello depends on is newer than the hello file, then it will execute the command defined later to generate the hello file

4) If the hello.o file that hello depends on does not exist, then make will find the dependency of the target hello.o file in the current file, and if found, then generate the hello.o file according to that rule

5) Of course, your C file and H file exist, so make will generate the hello.o file, and then use the hello.o file to declare the ultimate task of make, which is to execute the file hello

6) This is the dependency of the entire make. Make will find the dependencies of the files layer by layer until the first target file is finally compiled.

7) In the process of searching, if there is an error, such as the last dependent file cannot be found, then make will exit directly and report an error, and for the error of the defined command, or the compilation is unsuccessful, or make does not work at all. ignore

8) make only cares about the dependencies of the files, that is: if the files after the hair do not exist after finding the dependencies, then it will not work

9) The project is to be cleaned up

10) Like clean, which is not directly or indirectly associated with the first target file, then the commands defined after it will not be automatically executed. However, we can show that make executes, that is, the command: make clean, so as to Clear all object files for recompiling

11) Generally, for the target file we want to clean, we set it as a pseudo target and decorate it with .PHONY. The characteristics of the pseudo target are always executed.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326886259&siteId=291194637