vscode - Ubuntu - Makefile details (extremely easy to understand)

Keywords: Linux IDE Visual Studio Code

1. Makefile introduction

         There are countless source files in a c + + project, which are placed in several directories according to type, function and module. Makefile file defines a series of rules to specify which files need to be compiled first, which files need to be compiled later, which files need to be recompiled, and even more complex function operations, because makefile file is like a Shell script, You can also execute the commands of the operating system;

         The benefit of Makefile is "automatic compilation". Once written, only one make command is needed, and the whole project will be compiled automatically, which greatly improves the efficiency of software development. Make is a command tool that explains the zeroing in Makefile files. Generally speaking, most ides have this command, such as make in Delphi, make in Visual C + +, and make in GNU under Linux;

Note:

        (1) The file name can only be makefile or makefile;

        (2) makefile rule

                  A makefile can have one or more rules;

                  Target...: dependency

                          Command (Shell command)

                           ...

                  Target: refers to the name of the generated target file, which can be taken arbitrarily;

                  Dependency: generate files required by the target;

                  Command: generate the target for dependency operation by executing the command (Tab must be added before the command);

                  Other rules in makefile generally serve the first rule;

                        

2. Several common forms of Makefile files

         As shown in the figure below, it is a C I built under Linux++   When running the project under Linux, you need to enter the following instructions:

//Choose one from two
g++ main.cpp BPlusTree.cpp CLThread.cpp table.cpp -o main 

g++ main.cpp BPlusTree.cpp CLThread.cpp table.cpp -o main -lpthread

 

         If you create a Makefile file in this directory, enter make on the Linux terminal to run it; The contents of Makefile files can be in the following forms:

(1)makefile

app:main.cpp BPlusTree.cpp CLThread.cpp table.cpp
    g++ main.cpp BPlusTree.cpp CLThread.cpp table.cpp -o app

  Note: app is the generated target file name, which can be taken at will;

(2)makefile

app:main.o BPlusTree.o CLThread.o table.o
    g++ main.o BPlusTree.o CLThread.o table.o -o app

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

BPlusTree.o:BPlusTree.cpp
    g++ -c BPlusTree.cpp -o BPlusTree.o

CLThread.o:CLThread.cpp
    g++ -c CLThread.cpp -o CLThread.o

table.o:table.cpp
    g++ -c table.cpp -o table.o

(3)makefile

#Define variables
src=main.o BPlusTree.o CLThread.o table.o
target=app
$(target):$(src)
    $(CXX) $(src) -o $(target)

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

BPlusTree.o:BPlusTree.cpp
    g++ -c BPlusTree.cpp -o BPlusTree.o

CLThread.o:CLThread.cpp
    g++ -c CLThread.cpp -o CLThread.o

table.o:table.cpp
    g++ -c table.cpp -o table.o

(4)makefile

#Define variables
src=main.o BPlusTree.o CLThread.o table.o
target=app
$(target):$(src)
    $(CXX) $(src) -o $(target)

%.o:%.cpp
    $(CXX) -c $< -o $@

(5)makefile

src=$(wildcard ./*.cpp)                #Obtain all. cpp files in the current directory, which can have multiple format files;
objs=$(patsubst %.cpp, %.o, $(src))    #Replace the. cpp file with the. o file
target=app
$(target):$(objs)
    $(CXX) $(objs) -o $(target)

%.o:%.cpp
    $(CXX) -c $< -o $@


#Pseudo target. Delete the. o file after compilation
.PHONY:clean
clean:
    rm $(objs) -f

Note: the (5th) makefile is generally universal and does not need to be modified;  

3. Basic principles of Makefile

(1) Before executing the command, you need to check whether the dependency in the rule exists;

        a. If present, execute the command;

        b. If it does not exist, check its rules downward to check whether a rule is used to generate the dependency. If it is found, execute the commands in the rule;

(2) Detect the update. When executing the command in the rule, it will compare the time of the target and dependent files;

        a. If the dependent time is later than the target time, the target needs to be regenerated;

        b. If the dependent time is earlier than the target time, the target does not need to be updated, and the commands in the corresponding rules do not need to be executed;

4. Variables for Makefile

(1) Custom variable

        Variable name = variable value

var=hello

#Gets the value $(variable name) of the variable
$(var)

(2) Predefined variables

        Ar: the name of the archive maintenance program. The default value is ar;

         CC: the name of the C compiler. The default value is gcc;

        CXX: the name of the C + + compiler. The default value is g + +;

        $@: The full name of the target;

        S <: the name of the first dependent file;

        s ^: names of all dependent files;

        Note: the last three can only be used in commands;

app:main.cpp a.cpp b.cpp
    g++ -c main.cpp a.cpp b.cpp -o app

#Automatic variables can only be used in regular commands
app:main.cpp a.cpp b.cpp
    $(CC) -c $^ -o $@

5. Pattern matching for Makefile

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

BPlusTree.o:BPlusTree.cpp
    g++ -c BPlusTree.cpp

CLThread.o:CLThread.cpp
    g++ -c CLThread.cpp

table.o:table.cpp
    g++ -c table.cpp
%.o:%.cpp

# %: wildcard, matching a string; Two% matches the same string;

6. Functions of Makefile

(1)$(wildcard PATTERN...)

        Function: obtain the file list of the specified type in the specified directory;

        Parameter: PATTERN refers to the corresponding type of file in one or more directories. If there are multiple directories, space interval is generally used;

        Return: a list of several files obtained, with spaces between file names;

        Example:

                $(wildcard ./*.cpp)

                Return value format: main.cpp BPlusTree.cpp CLThread.cpp table.cpp

(2)$(patsubst <pattern>, <replacement>, <text>​​​​​​​)

        Function: find out whether the words in < text > (separated by "space", "Tab" or "carriage return" and "line feed") conform to the pattern < pattern >, and if they match, replace them with < replacement >;

        < Pattern > can include wildcards%, representing strings of any length. If  < If the replacement > also contains%, then  < This% in replacement > will be the string represented by the% in < pattern >. (you can escape with \, and use \% to represent the% character of the real meaning)

        Return: the function returns the replaced string;

         Example:

                $(patsubst %.cpp, %.o, $(src))

                Return value format: main.o BPlusTree.o CLThread.o table.o

                

Posted by jrough on Fri, 03 Dec 2021 13:50:44 -0800