Easy-to-understand Makefile Getting Started (01) — What is Makefile, why use Makefile, Makefile rules, and how to achieve incremental compilation in Makefile process

1. What is Makefile

MakefileDocument describes Linuxthe system C/C++compilation rules of the project, which is used to compile the automation C/C++project. Once the writing is good writing Makefilepapers, only one makecommand, the whole project will begin automatically compiled, no longer need to manually execute GCCthe command. In a large C/C++source files of the project there are hundreds, according to their functions, modules, types were placed in different directories, Makefilefile defines a set of rules that indicates the order of compilation of the source file dependencies, the need to re Compile and so on.

MakefileIt can be simply regarded as the compilation rules of a project file, describing the compilation and linking rules of the entire project. It includes those files that need to be compiled, those files that do not need to be compiled, those files that need to be compiled first, those files that need to be compiled later, those files that need to be rebuilt, and so on. Compile the entire project needs involved, in Makefileboth can be described. In other words, it Makefilecan make the compilation of our project project automated, without having to manually input a bunch of source files and parameters every time.

2. Why use Makefile

To Linuxlower the Clanguage development as an example to explain the specific, multi-file compiler generates a file, the compiler command is as follows:

gcc -o outfile name1.c name2.c ...

outfileThe name of the executable program to be generated is the name nameN.cof the source file. This is what we Linuxuse at gcccompiler to compile Cthe sample files. If the number of source files we encounter is not very large, we can choose this compilation method. If there are too many source files, you will encounter the following problems.

2.1 The problem of linking libraries when compiling

Here are some standard libraries that we need to link manually:

  • name1.cUses math library mathfunctions, we have to manually add parameters -lm;
  • name4.cUse a small database SQLitefunction, we have to manually add parameters -lsqlite3;
  • name5.cWhen threads are used, we need to manually add parameters -lpthread;

Because there are many files, many third-party libraries have to be linked. So the command will be very long when compiling, and we may be involved in the order of file linking when compiling, so manual compilation will be very troublesome.

If we learn to use Makefilenot the case, it would radically simplify the operation of the compiler. To link the library file in Makefile, the development of appropriate rules and corresponding link order. So just need to execute makethe command, the project will automatically compile a manual compilation of parameter options and commands omitted, very convenient.

2.2 Compiling large projects will take a long time

MakefileSupporting multi-threaded concurrent operation will greatly shorten our compilation time, and when we modify the source file, when compiling the entire project, the makecommand will only compile the files we have modified, and the unmodified files do not need to be recompiled. Greatly solved our time-consuming problem.

And file Makefileonly needs to be done once, usually we just do not add a file or delete the project, Makefilebasically do not have to modify, compile-time use only one makecommand. It provides us with great convenience and greatly improves the efficiency of compilation.

3. Makefile rules

Its rules are mainly composed of two parts, the dependency relationship and the executed command, and its structure is as follows:

targets : prerequisites
    command

or

targets : prerequisites; command
    command

The relevant instructions are as follows:

  • targets: The target of the rule is a must, it can be Object File(generally called an intermediate file), it can be an executable file, or it can be a label;
  • prerequisites: Our dependence file to generate targetsa file or a desired target. There can be multiple, separated by spaces, or none;
  • command: makeCommand (you need to perform any of the shellcommands). There can be multiple commands, each command occupies one line;

If commandtoo long, you can use \a newline.

Note: To use a colon between our goals and dependent files are separated, the start command must use the Tabkeys, you can not use the spacebar.

To briefly summarize Makefilethe content in, it mainly contains five parts, namely:

3.1 Explicit rules

Explicit rules explain how to generate one or more object files. This is caused by Makefilethe writers clearly pointed out that the resulting file, file dependencies, generated commands.

3.2 Obscure rules

As our makename with automatic derivation function, so obscure rules allow us to briefly write rough ground Makefile, which is makesupported command.

3.3 Definition of variables

In the Makefilemiddle we have to define a set of variables, which are generally strings, this is a bit like the C language macros, when Makefileexecuted, where the variables will be extended to the corresponding reference position.

3.4 Document instructions

Which comprises three parts, one is a Makefilereference to the other Makefile, as in the C language includeas; refers to the other cases in accordance with certain specified Makefilevalid portion, as precompiled C language #ifthe same; and It is to define a multi-line command.

3.5 Notes

MakefileOnly in-line comments, and UNIXof Shellthe same scripts which the comment is #characters, if you want your Makefileuse of #characters can be escaped by a backslash, such as: \#.

3.6 Wildcards in rules

  • * Represents any one or more characters
  • ? Represents any character
  • [...] [abcd] represents any character in a, b, c, d, [^abcd] represents a character other than a, b, c, d, [0-9] represents any number from 0 to 9
  • ~ Represents the user's home directory

4. Makefile example

main.cpp Code:

#include <iostream>

int main()
{
    
    
    std::cout << "hello,world" << std::endl;
    return 0;
}

Specifically by the following examples about the use of Makefilerules Makefilefile, add the following code:

main: main.cpp
	g++ main.cpp -o main

Which mainis the target file, and our resulting executable file. Dependent files that main.cppoperation of the source files, object files need to do to rebuild that g++ main.cpp -o main. This is the Makefileuse of basic grammar rules.

Use Makefileway: First you need to write a good Makefilefile, and then execute the shell makecommand, the program will automatically execute, get the final destination file.

wohu@ubuntu:~/cpp/demo$ ls
main.cpp  `Makefile`
wohu@ubuntu:~/cpp/demo$ make 
g++ main.cpp -o main
wohu@ubuntu:~/cpp/demo$ ls
main  main.cpp  `Makefile`
wohu@ubuntu:~/cpp/demo$ ./main 
hello,world
wohu@ubuntu:~/cpp/demo$ 

If the space bar is used at the beginning of the command, an error will be reported

Makefile:2: *** missing separator.  Stop.

Makefile: 2 for the second line error, should Tabbegin.

5. Makefile process

When we execute makewhen the command, makewill be performed to look for compiling the rules under the current file, which is Makefilefile. We write the Makefilename of the file when you can use GNUMakefile, makefile, Makefile, maketo go back to look for when performing Makefilefile, find the file sequence is like this.

Recommended to use Makefile(usually written in this way in the project, the uppercase will be more standardized). If the file does not exist, an makeerror will be reported to us, prompting:

make: *** No targets specified and no `Makefile` found.  Stop.

In Makefileadding the following code:

main: main.o   name.o greeting.o
	g++ main.o name.o greeting.o -o main
main.o: main.cpp
	g++ -c main.cpp -o main.o
name.o: name.cpp
	g++ -c name.cpp -o name.o
greeting.o: greeting.cpp
	g++ -c greeting.cpp -o greeting.o

When we compile the project file, by default, makeexecution is the Makefilefirst rule (the Makefilefirst dependencies appear), the first objective of this rule is called the "ultimate goal" or "ultimate goal" .

In the shellcommand line of the makecommand, you can get executable files mainand intermediate files main.o, name.oand greeting.o, mainthat is our final document to be generated.

By Makefilecan find our target mainin Makefilethe first goal, so it is the makeultimate goal, when the modified any files, execute makewill rebuild the ultimate goal main.

Its specific work order is: when shellthe input prompt makesubsequent command. makeRead in the current directory Makefilefiles, and Makefilefile first goal as "ultimate goal" of its execution, start processing the first rule (rule where the ultimate goal).

In our example, the first rule is the goal of mainthe rules is located. Rules describe the maindependencies and define the link .ofile generation target maincommand; makebefore executing this command rules defined by the first processing target mainall dependent files (those examples of the .ofile) update rule (these .ofiles Target rules).

These .otargeting rules dealing with the following three cases file:

  • Target .ofile does not exist, create it using its description rules;
  • Target .ofile exists, the target .ofile is dependent on ".cpp" source ".h" file of any target ratio .o"Update" file (last make after being modified), then re-compiled rules according to which;
  • Target .ofile exists, the target .ofile than any one of its dependencies ( ".c" source, ". H" file) "Update" (It depends on the file has not been modified after a make over), do nothing ;

Through the above update rules we can understand the role of the intermediate file, which is generated when compiling .othe file. The function is to check whether a certain source file has been modified, and whether the final target file needs to be rebuilt.

We execute makecommand, only the modified source files or nonexistent target file will be rebuilt, but those files are not changed without recompiling, so to save time in large part, to improve programming efficiency.

Guess you like

Origin blog.csdn.net/wohu1104/article/details/110905996