Linux Operating System Experiment 12 Linux Programming Technology Application

1. Experimental purpose and requirements

  1. Use Makefile to manage and compile source code, and master the basic syntax of Makefile.
  2. Learn the definition and use of variables in Makefile, as well as the common use of special variables.
  3. Learn the automatic derivation function of makefile.
  4. Learn to use makefile pseudo-targets.

2. Experimental platform

The experimental environment installed in the laboratory (Linux operating system) and Touge (www.educoder.net) experimental platform (course experiment)

3. Experimental content

  1. Basic syntax of Makefile.
  2. Definition and use of variables in Makefile
  3. Automatic derivation function of makefile
  4. pseudo-targets for makefiles

4. Experiment details and steps

mission details

The task of this level: use Makefileto manage the compiled source code and master Makefilethe basic grammar.

related information

what is makefileit Maybe many Winodwsprogrammers don't know this thing, because those Windowspeople IDEhave done this work for you, but to be a professional programmer, you makefilestill need to understand it. makefileIn fact, it describes the compilation order and compilation rules of all files in the entire project, and makereads makefilethe files by commands, and then makefileparses them according to the rules defined in the files to complete the compilation operation of the entire project.

makefilelinuxIt is relatively common in operating systems. For example, when we use source code to install a software, we usually only need to execute commands maketo complete the compilation of the software. It is precisely because the software developer has written makefilethe file, so we only need to execute makethe command The automatic compilation of the entire project will be completed.

makefileThe grammar introduced in this pass is used makefileto complete the compilation of the software.

Makefile rules

makefileThe file contains a set of rules for compiling the application. A rule can be divided into three parts:

  1. work target
  2. Dependency (prerequisite)
  3. The command to be executed (command)

The format is:

  1. target : prereq1 prereq2
  2.     commands

The above format is the dependency relationship of a file, that is to say, targetthis object file depends on multiple prerequisitesfiles, and its generation rules are defined in commands. To put it bluntly, prerequisitesif there is more than one file in the file targetthat is newer than the file, commandsthe defined command will be executed. That's Makefilethe rule. That is Makefilethe most core content.

Notice

  1. The TAB key is used in front of the commands instead of a space, and an error will occur if a space is used;
  2. commands can be any shell command;
  3. When executing the make command, make will parse the first rule;

Case demo 1:

There is a source code main.cfile, compile a makefilerule to compile the file, and generate an HelloWorldexecutable file named as follows:

  1. vim makefile
  2. make
  • vimWrite the following code using
  1. #include <stdio.h>
  2. int main()
  3. {
  4.     printf("Hello world\n");
  5.     return 0;
  6. }
  • use vimwritemakefile
  1. HelloWorld : main.c
  2.     gcc -o HelloWorld main.c

[Please experience directly in the "command line" on the right]

As can be seen from the above cases, after writing makefile, you only need to enter makethe command and automatically only need to define the rules.

Note: gcc -o HelloWorld main.cthe command is preceded by TABa key and not a space.

Case demo 2:

Assuming that a project contains 5source code files, namely Add.c, Sub.c, Mul.c, Div.cand main.cand a header file def.h, compile a makefilerule to compile the project and generate an exeexecutable file named , the specific operations are as follows:

  1. vim makefile
  2. make
  • vim Add.c
  1. #include <stdio.h>
  2. int Add(int a, int b)
  3. {
  4.     return a + b;
  5. }
  • vim Sub.c
  1. #include <stdio.h>
  2. int Sub(int a, int b)
  3. {
  4.     return a - b;
  5. }
  • vim Mul.c
  1. #include <stdio.h>
  2. int Mul(int a, int b)
  3. {
  4.     return a * b;
  5. }
  • vim Div.c
  1. #include <stdio.h>
  2. int Div(int a, int b)
  3. {
  4.     return a /  b;
  5. }
  • vim main.c
  1. #include <stdio.h>
  2. #include "def.h"
  3. int main()
  4. {
  5.     int add = Add(10, 5);
  6.     int sub = Sub(10, 5);
  7.     int mul = Mul(10, 5);
  8.     int div = Div(10, 5);
  9.     
  10.     printf("10 + 5 = %d\n", add);
  11.     printf("10 - 5 = %d\n", sub);
  12.     printf("10 * 5 = %d\n", mul);
  13.     printf("10 / 5 = %d\n", div);
  14.     
  15.     return 0;
  16. }
  • vim def.h
  1. #ifndef __DEF_H__
  2. #define __DEF_H__
  3. #include <stdio.h>
  4. int Add(int a, int b);
  5. int Sub(int a, int b);
  6. int Mul(int a, int b);
  7. int Div(int a, int b);
  8. #endif
  • vim makefile
  1. exe : main.o Add.o Sub.o Mul.o Div.o
  2.     gcc -o exe main.o Add.o Sub.o Mul.o Div.o
  3. main.o : main.c def.h
  4.     gcc -c main.c -o main.o
  5. Add.o : Add.c
  6.     gcc -c Add.c -o Add.o
  7. Sub.o : Sub.c
  8.     gcc -c Sub.c -o Sub.o
  9. Mul.o : Mul.c
  10.     gcc -c Mul.c -o Mul.o
  11. Div.o : Div.c
  12.     gcc -c Div.c -o Div.o

[Please experience directly in the "command line" on the right]

In the above case, when only makethe command is needed, the rule with the target is first parsed exe, and then exethe dependencies are found, and then the rules are parsed main.o、Add.o和Sub.oseparately , that is, the commands with the target are executed respectively. When generated, finally execute the corresponding command.main.o、Add.o和Sub.omain.o、Add.o和Sub.omain.o、Add.o和Sub.oexe

programming requirements

The task of this level is to learn to use makefileto compile the project.

The specific programming requirements are as follows:

  • Write 5a source file Add.c, Sub.c, Mul.c, Div.c and main.c and a header file def.h, the file content is the same as the case 2;
  • Use makefileto manage the above projects, and compile and generate an Calcexecutable file named;
  • vim Add.c
1.	#include <stdio.h>
2.	int Add(int a, int b)
3.	{
4.	    return a + b;
5.	}
  • vim Sub.c
1.	#include <stdio.h>
2.	int Sub(int a, int b)
3.	{
4.	    return a - b;
5.	}
  • vim Mul.c
1.	#include <stdio.h>
2.	int Mul(int a, int b)
3.	{
4.	    return a * b;
5.	}
  • vim Div.c
1.	#include <stdio.h>
2.	int Div(int a, int b)
3.	{
4.	    return a /  b;
5.	}
  • vim main.c
1.	#include <stdio.h>
2.	#include "def.h"
3.	int main()
4.	{
5.	    int add = Add(10, 5);
6.	    int sub = Sub(10, 5);
7.	    int mul = Mul(10, 5);
8.	    int div = Div(10, 5);
9.	
10.	    printf("10 + 5 = %d\n", add);
11.	    printf("10 - 5 = %d\n", sub);
12.	    printf("10 * 5 = %d\n", mul);
13.	    printf("10 / 5 = %d\n", div);
14.	
15.	    return 0;
16.	}
  • vim def.h
1.	#ifndef __DEF_H__
2.	#define __DEF_H__
3.	
4.	#include <stdio.h>
5.	
6.	int Add(int a, int b);
7.	int Sub(int a, int b);
8.	int Mul(int a, int b);
9.	int Div(int a, int b);
10.	#endif
  • vim makefile
1.	Calc : main.o Add.o Sub.o Mul.o Div.o
2.	    gcc -o Calc main.o Add.o Sub.o Mul.o Div.o
3.	
4.	main.o : main.c def.h
5.	    gcc -c main.c -o main.o
6.	
7.	Add.o : Add.c
8.	    gcc -c Add.c -o Add.o
9.	
10.	Sub.o : Sub.c
11.	    gcc -c Sub.c -o Sub.o
12.	
13.	Mul.o : Mul.c
14.	    gcc -c Mul.c -o Mul.o
15.	
16.	Div.o : Div.c
17.	    gcc -c Div.c -o Div.o

mission details

The task of this level: Makefilethe definition and use of variables in learning, and the use of commonly used special variables.

related information

The use of variables can facilitate us to write makefilefiles. We can use a short variable to replace a longer string, so that we can easily use variables to replace when using this string. In addition, when we modify a certain string, if we do not use a variable, we need to modify every place where the string is used. If a variable is used, we only need to modify the variable definition.

makefileVariable commands can contain characters, numbers, underscores (may start with numbers), and are case-sensitive.

makefile$Variables need to be assigned a value when they are declared, and ** symbols need to be added before the variable name when using the variable. For example $(VARNAME), if the user needs to makefileuse real $characters in the file, use $$** to indicate.

makefileThere are three ways to assign values ​​​​to variables in , namely:

  1. Recursive assignment (=): recursive assignment, that is, the assignment does not take effect immediately, and the actual assignment is made when it is used. At this time, the current value is found through recursion;
  2. Direct assignment (:=): It is to directly expand the variable contained in the right side of ":=" to the variable assignment on the left side;
  3. Conditional assignment (?=): Only when this variable has not been assigned a value before, this variable will be assigned a value, so it is generally used for the first assignment;

makefileIn addition to custom variables, there are also some system default special variables, which can help us quickly write makefilefiles, such as: $@、$<和$^etc.

This level will introduce makefilethe definition and use of variables, as well as the use of special variables to write makefilefiles.

Makefile custom variables

Custom variable format:

  • recursive assignment 变量名 = 变量内容
  • direct assignment 变量名 := 变量内容
  • conditional assignment 变量名 ?= 变量内容

Variables are used in the format: $变量名either ${ 变量名}or$(变量名)

Case demo 1:

The item in the case in the previous level 2contains 5a source code file and a header file. If you use variables to write, makefileit will show a relatively concise format. The specific operation is as follows:

  1. vim makefile
  2. make
  • vim makefile
  1. object=main.o Add.o Sub.o Mul.o Div.o
  2. exe : $(object)
  3.     gcc -o exe $(object)
  4. main.o : main.c def.h
  5.     gcc -c main.c -o main.o
  6. Add.o : Add.c
  7.     gcc -c Add.c -o Add.o
  8. Sub.o : Sub.c
  9.     gcc -c Sub.c -o Sub.o
  10. Mul.o : Mul.c
  11.     gcc -c Mul.c -o Mul.o
  12. Div.o : Div.c
  13.     gcc -c Div.c -o Div.o

[ makefilecontent]

[Please experience directly in the "command line" on the right]

It can be seen that we use objectto represent main.o Add.o Sub.o Mul.o Div.o, so that we can use $(object)to represent the above target file instead of entering 5this .

Makefile special variables

makefileCommonly used special variables are:

  1. $@: indicates all targets;
  2. $^: Indicates the collection of all dependent targets, separated by spaces;
  3. $<: Indicates the name of the first target in the dependent target;

Case demo 1:

Following the project in the previous case, if it is written using special variables, makefileit will display a more concise format. The specific operations are as follows:

  1. vim makefile
  2. make
  • vim makefile
  1. object=main.o Add.o Sub.o Mul.o Div.o
  2. exe : $(object)
  3.     gcc -o $@ $(object)
  4. main.o : main.c def.h
  5.     gcc -c $< -o $@
  6. Add.o : Add.c
  7.     gcc -c $< -o $@
  8. Sub.o : Sub.c
  9.     gcc -c $< -o $@
  10. Mul.o : Mul.c
  11.     gcc -c $< -o $@
  12. Div.o : Div.c
  13.     gcc -c $< -o $@

[Please experience directly in the "command line" on the right]

programming requirements

The task of this level is to learn to use makefileto compile the project.

The specific programming requirements are as follows:

  • Write 5a source file Add.c, Sub.c, Mul.c, Div.c and main.c and a header file def.h, the file content is the same as the first case 2;
  • The variable used makefileto compile produces an VarCalcexecutable named;

•    vim makefile

1.	object=main.o Add.o Sub.o Mul.o Div.o
2.	VarCalc : $(object)
3.	    gcc -o VarCalc $(object)
4.	
5.	main.o : main.c def.h
6.	    gcc -c $< -o $@
7.	
8.	Add.o : Add.c
9.	    gcc -c $< -o $@
10.	
11.	Sub.o : Sub.c
12.	    gcc -c $< -o $@
13.	
14.	Mul.o : Mul.c
15.	    gcc -c $< -o $@
16.	
17.	Div.o : Div.c
18.	    gcc -c $< -o $@

mission details

The task of this level: makefilethe automatic derivation function of learning.

related information

make.oVery powerful, it can automatically deduce the commands behind files and file dependencies, so we don't need to write similar commands after each file. Because, ours makewill automatically recognize and derive commands by ourselves.

As long as makeit sees a .ofile, it will automatically add .cthe file to the dependencies, and if makeit finds one main.o, then main.cit will be main.othe dependent file. And gcc -c main.cwill also be derived, so we makefiledon't have to write so complicated anymore.

This level will introduce makefilethe automatic derivation function.

Makefile automatic derivation

Automatic derivation format:目标 : 其它依赖

Case demo 1:

If you use the automatic derivation mode to write the case in the previous level makefile, there will be a more concise format, the specific operation is as follows:

  1. vim makefile
  2. make
  • vim makefile
  1. object=main.o Add.o Sub.o Mul.o Div.o
  2. exe : $(object)
  3.     gcc -o $@ $(object)
  4. main.o : def.h

[Please experience directly in the "command line" on the right]

It can be seen that we only need to main.ocreate a compilation rule for 4the target file, and there is no need to create a compilation rule for it, because makethe compilation rule will be automatically constructed for it.

programming requirements

The task of this level is makefilean automatic derivation in learning. The specific programming requirements are as follows:

  • Modify the previous level makefile, use automatic derivation to compile the project, and generate an AutoCalcexecutable file named;

•    vim makefile 

1.	object=main.o Add.o Sub.o Mul.o Div.o
2.	AutoCalc : $(object)
3.	    gcc -o AutoCalc $(object)
4.	
5.	main.o : def.h

mission details

The task of this level: learn to use makefilethe false target.

related information

A rule for clearing object files (.o and execution files) should be written in each Makefile, which is not only convenient for recompilation, but also very helpful for keeping files clean.

make installUsually, when we use source code to install software, we will execute this command to install the software after compiling the software , or execute make cleanthis command to clear the temporarily generated target files. The above operation is the use of makefilethe false target.

The false target that will be introduced in this level makefile.

Makefile pseudo-target

makefileUse .PHONYkeywords to define a pseudo-target, the specific format is:.PHONY : 伪目标名称

Case demo 1:

makefileClear the temporary object file label for the addition in the previous level case clean, the specific operation is as follows:

  1. vim makefile
  2. make
  • vim makefile
  1. object=main.o Add.o Sub.o Mul.o Div.o
  2. exe : $(object)
  3.     gcc -o $@ $(object)
  4. main.o : def.h
  5. .PHONY : clean
  6. clean :
  7.     rm $(object)

[Please experience directly in the "command line" on the right]

It can be seen that when we execute makethe command, multiple temporary files will be generated, and then make cleanafter we execute the command, the generated temporary file will be deleted. In fact, executing make cleanthe command is executing rm main.o Add.o Sub.o Mul.o Div.o.

Case demo 2:

Use another format to clear the temporarily generated directory files and not display the delete command, the specific operation is as follows:

  1. vim makefile
  2. make
  • vim makefile
  1. object=main.o Add.o Sub.o Mul.o Div.o
  2. exe : $(object)
  3.     gcc -o $@ $(object)
  4. main.o : def.h
  5. clean :
  6.     @echo "clean object files"
  7.     @rm $(object)

[Please experience directly in the "command line" on the right]

As you can see, when we execute make cleanthe command, the command will not be displayed in the terminal rm main.o Add.o Sub.o Mul.o Div.o.

Note: If the ** ** symbol is added before the command @, the command will not be output to the terminal as it is.

programming requirements

The task of this level is makefilea false goal in the society. The specific programming requirements are as follows:

  • makefileAdd a pseudo-target that automatically cleans up temporary files for the previous level clean, and generate an AutoCleanCalcexecutable file named ;

•    vim makefile 

1.	object=main.o Add.o Sub.o Mul.o Div.o
2.	AutoCleanCalc : $(object)
3.	    gcc -o AutoCleanCalc $(object)
4.	
5.	main.o : def.h
6.	
7.	.PHONY : clean
8.	
9.	clean :
10.	    rm $(object)

Guess you like

Origin blog.csdn.net/qq_64314976/article/details/131432211