Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Make-- implicit Rule path search and its example

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

I. implicit rules

question one

.PHONY : allall: @echo "command-1"VAR:=testall: @echo "all:$(VAR)"

The result after make is as shown in the figure

it can be concluded

A. When a target with the same name appears in the makefile

Dependency: All dependencies will merge together to become the final dependency of the target

Command: make warns when the same target appears in multiple places; all previously defined commands are replaced by the last defined command

Note: When using the include keyword to include other files, you need to ensure that the target with the same name in the included file has only dependencies and no commands; otherwise, the command with the target with the same name will be overwritten.

implicit rules

1.make provides some common, routine rule implementations

2. make tries implicit rules when rules for the corresponding target are not provided

3. make provides implicit rules for generating target files

4. Implicit rules use predefined variables to complete compilation

5. Changing predefined variables will partially change the behavior of implicit rules

6. Implicit rules are no longer used when custom rules exist

Deep understanding of implicit rules

A. When the dependency of make send target does not exist

1. Try to find implicit rules one by one by dependency name

2. and deduces source files that may be needed by dependency names

Side effects of implicit rules

1. Compilation behavior is hard to control--heavy use of implicit rules can produce unexpected compilation behavior

2. Compilation inefficiency--make selects end-use rules from implicit rules and custom rules

implicit rule chain

When the dependent target does not exist, make tries to combine various implicit rules to create the target, resulting in unexpected compilation behavior---requiring a target named N.o:N.y->N.c>N.o

View Implicit Rules

Disablement of implicit rules

A. Partial prohibition

1. Custom rules in makefile

2. In makefile in definition mode (eg:%.o:%.p)

B. Global disablement

make -r

Introduction to Suffix Rules

1. Suffix rules are old-fashioned "pattern rules"

2. Rules can be customized by suffix description

Double suffix rule--defines a pair of file suffixes (dependent file suffix and target file suffix), such as: .cpp%.o:%.cpp

Single suffix rule--defines a single file suffix (source file suffix), e.g..c%: %.c

Notes on Suffix Rules

1. Dependency is not allowed in suffix rules

2. Suffix rules must have commands, otherwise meaningless

3. Suffix rules are gradually replaced by pattern rules

Summary:

1. When multiple commands for the same target appear, only the last defined command is valid

2. make provides a series of implicit rules that can be used

3. Try using implicit rules when no rules are defined in the makefile

4. Implicit rules may use predefined variables in make

5. Changing predefined variables can partially change the behavior of predefined rules

6. Implicit rules can cause unexpected compilation behavior

7. Implicit rules should not be used in practical engineering projects

8. The suffix rule is an old-fashioned pattern rule

9. Suffix rules are gradually being replaced by pattern rules

II. path search

A. Common source code management methods

The makefile in the project must be able to correctly locate the source files and dependent files, and finally compile the executable program code as shown below

As shown in the figure: inc folder is the header file, and src folder is the source file, makefile documentation program as shown in the figure, make after the result as shown in the figure

This result is generated because func.c does not exist in the current folder

Special predefined variable VPATH(all caps)

The value of the VPATH variable is used to indicate how make finds files.

2. Different folders can appear simultaneously as VPATH values

3. Folder names need to be distinguished by delimiters

make What to do with VPATH values

1. VPATH is used when the current file cannot find the required file.

2. make searches for files in the folder specified in VPATH

3. When multiple folders with the same name exist, select the file found first time

Notes:

1. VPATH can only determine the search path of make, but cannot determine the search path of command.

2. For a specific compile command (gcc), the compile search path needs to be specified independently

3.

The modification to the previous example and the running result are shown in the figure

VPATH problems--compile errors may occur when source files (C/Cpp files) unexpectedly appear in the inc folder

Alternative: vpath keyword (all lowercase)

1. Specify different search paths for different file types

2. Syntax--Search Directory for files that match Pattern's rules

Examples of operation and operation results are shown in the figure

Cancel Search Rules:

1. Cancel a search rule that has been set

.vpath Pattern

2. Cancel all rules that have been set

.vpath

Several Problems of Three-path Search

Question 1: What does make do when VPATH and vpath appear together?

Examples of operation and result diagrams

When you change func.c to func.cpp in src2, make again.

the result of the operation of

Comparison of conclusions

1.make First search the current folder for the desired file

2. if it fails

a.make searches for target files in the folder specified by vpath first

b. When vpath search fails, search the folder specified by VPATH instead.

Question 2--What does make do when using vpath to specify multiple folders for the same Pattern

Examples and running results are shown in the figure

Conclusion:

1.make First search the current folder for the desired file

2. if it fails

a.make searches the folder specified by vpath from top to bottom

b. When the target file is found, the search ends

Question 3-How does make determine the final location of the target file after specifying the search path through VPATH

Put the app.out generated by the first run in the src folder. Run the make result as shown in the figure

Conclusion:

1. When app.out doesn't exist at all--make Create app.out in the current folder

2. When app.out exists in src folder--all targets and dependencies remain the same old and new, make does not recreate app.out. When dependency files are updated, make creates app.out in the current folder

Four Summaries:

The VPATH variable tells make how to find files.

2. make searches for files in the folder specified in VPATH

The vpath keyword specifies different search paths for different types of files

4.vpath is more flexible and easy to use than VPATH, and search paths can be dynamically set/cancelled.

5. Use vpath whenever possible to specify search paths for different files

6. Do not generate object files in source folders

7. Create a separate folder for compiled results

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report