In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
15.Make 's implicit rule 15.1. Command override
Question1: what happens if you split and write to different places through the command of each target?
.PHONY: all VAR: = testall: @ echo "all: $(VAR)" include 1.mk
The contents of the file 1.mk:
All: @ echo "this is command from 1.mk"
When a target with the same name appears in Makefile:
Dependency: all dependencies will be merged together to become the ultimate dependency of the goal
Commands: when commands for the same target appear in multiple places, make issues a warning, and all previously defined commands are replaced by the last command.
Note:
When using include to include other files (makefile), you need to make sure that the target of the same name in the included file has only dependencies and no commands; otherwise, the command of the target with the same name will be overwritten!
15.2. Implicit rule
Some common, routine rule implementations are provided in Make, and make tries to use implicit rules when the target rule is not provided
Can the following code be compiled successfully? Why?
SRCS: = $(wildcard * .c) OBJS: = $(SRCS:.c=.o) app.out: $(OBJS) $(CC)-o $@ $^ $(RM) $^ @ echo "Target = = > $@"% .o:% .c @ echo "my rule" $(CC)-c-o $@ $^
According to our preliminary analysis, there are at least two problems in the above Makefile:
1. Instead of defining CC and RM variables, use the
two。 No rules are defined for generating .o files (only links are not compiled)
Running result:
The reason why it can run successfully is due to the implicit rules in make.
Implicit rules for generating object files are provided in Make, which use predefined variables to complete the compilation.
Change the behavior of partially changing implicit rules between predefined variables, and implicit rules are no longer used when there are custom rules.
16.Make 's implicit rule 16.1. The disadvantages of implicit rules
When the dependency of the target does not exist, make tries to find the implicit rules one by one through the dependency name and derives the possible source file from the dependency name.
This behavior seems to simplify the way makefile is written, but it can cause unexpected problems.
Implicit rule side effects:
Compilation behavior is difficult to control, and extensive use of implicit rules may lead to unexpected compilation behavior.
Inefficient compilation: make chooses the final rule from implicit and custom rules
Examples are as follows:
Makefile:
App.out: main.o func.o $(CC)-lstdc++-o $@ $^
Main.c:
# include extern void greeting (); int main () {greeting (); return 0;}
Since we did not define the greeting function, we guessed that a link error would be reported at compile time and the symbol greeting could not be found.
Actual output:
We see that the func.p file is directly used to generate the func.o file here, but the error is reported because the software pc is not installed in my environment. The whole process is completely different from what we expected.
Implicit rule chain:
When the dependent object file does not exist, make will try to combine various implicit rules to create the target, resulting in unexpected compilation behavior!
Example: need a goal named N.O: N.Y-- > N.C-- > N.o
We can use the make-p command directly to view all the implicit rules
16.2. Prohibition of implicit rules
1. Partial disable: customize rules in Makefile, such as defining a rule that has only targets and dependencies, but no commands, such as:
% .o:% .p
This allows us to use our custom rules instead of implicit rules
two。 Globally disabled
Make-r
Obviously, this kind of simple and practical, it is recommended to use.
16.3. Suffix rule
Suffix rules are old-fashioned "pattern rules", which can be customized by suffix description.
1. Double suffix rule: define a pair of file suffixes (dependent file suffix and target file suffix)
For example, .CPP% .o:% .cpp
two。 Single suffix rule: define a single file suffix (source file suffix)
For example, .c%:% .c
Note:
Dependency is not allowed in suffix rules, and suffix rules must have commands, otherwise they are meaningless.
It has been gradually replaced by pattern rules. It is recommended to use pattern rules directly.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.