Preliminary introduction to automatic dependency in makefile
Source: Internet
Author: User
Preliminary introduction to linux programming: Automatic dependency in makefile-general Linux technology-Linux programming and kernel information. The following is a detailed description. I recently learned about makefile and I don't know much about automatic dependency. By reading info make and some articles on the internet, I did some tests, therefore, I want to write down some learning processes to help beginners like me. There may be many errors. Please correct them.
Yeahnix, 04/5/26
1. Dependency and automatic dependency
Makefile execution is based on dependency. That is, if a target is to be generated, the file on which the target depends must exist or can be constructed according to some rules, then generate the Target Based on the rules for constructing the target. Rules in makefile can be divided into explicit rules, static rules, pattern rules, and implicit rules (for details, see info make ).
In Makefile, our dependency may need to contain a series of header files, for example, if our main. c contains the "# include" defs. h "", then our dependency should be:
Main. o: main. c defs. h
However, if it is a relatively large project, you must know which C files contain header files, and when you add or delete header files, you also need to carefully modify the Makefile, this is a very difficult task to maintain. To avoid this heavy and error-prone problem, we can use a function of the C/C ++ compiler. Most C/C ++ compilers support a "-M" option, that is, to automatically find the header file contained in the source file and generate a dependency. For example, if we execute the following command:
Cc-M main. c
The output is:
Main. o: main. c defs. h
So the dependencies automatically generated by the compiler do not have to manually write the dependencies of several files, but are automatically generated by the compiler. Note that if you use the gnu c/C ++ compiler, you must use the "-MM" parameter. Otherwise, the "-M" parameter also contains header files of some standard libraries.
So how can the dependencies generated by the compiler be automatically included in the makefile? By reading some articles on the Internet and info make, I have seen three methods: info make's sed method, depend method, and compiler command-MD method.
2. Automatic dependency in info make and problems encountered
The sed method is mentioned in info make:
Generate a Makefile named "name. d" for each "name. c" file. The [. d] File Stores the dependency of the corresponding [. c] file.
Therefore, we can write [. c] file and [. d] File dependency, and make auto update or self-contained [. d] file and include it in our main Makefile. In this way, we can automatically generate dependencies for each file.
The following Pattern Rule generates the [. d] file:
%. D: %. c
Set-e; $ (CC)-M $ (CPPFLAGS) $ <\
| Sed's/\ ($ * \) \. o [:] */\ 1.o $ @:/G' >$ @;\
[-S $ @] | rm-f $ @
This rule means that all [. d] files depend on the [. c] file, and the set-e specifies that the shell will exit when an error occurs. Sed command to replace the dependency:
Main. o: main. c defs. h // 1
To: // 1
Main. o main. d: main. c defs. h // 1
Therefore, the [. d] file is automatically updated and automatically generated. [-S $ @] | rm-f $ @ indicates deleting the generated empty. d file to reduce unnecessary processing.
Then use the "include" command of Makefile,
Sources = foo. c bar. c
Include $ (sources:. c =. d)
In the preceding statement, "$ (sources :. c =. d) ". c =. d "means to replace all the variables $ (sources [. c] strings are replaced [. d]. In this way, when include finds no. d files are automatically generated. d file. d file generation rules are generated. o file. c and. the dependency of the H file.
At the beginning, I did not have a good understanding of the functions of the // 1 section described in info make, and I did not understand the automatic update of the. d file, so I decided to test it. A simple program is compiled as follows:
[Root @ yeahnix test_makefile] # pwd
/Root/Projects/test_makefile
[Root @ yeahnix test_makefile] # ls-R
.:
Aa. c bb. c bb. h dir Makefile
./Dir:
Echomsg. c echomsg. h
Aa. c calls bb. c, and bb. c calls echomsg. c.
At the beginning, echomsg. c and echomsg. h is not in the dir directory, so the makefile is constructed using the above rules. It is normal that the program can be created ,. the d file also came out (as shown in info make. d. The file should be used as an intermediate file and can be automatically deleted. no matter whether the file is deleted or not, the program is released.) But make will report an error, aa. d Not found ......, later, I re-read info make and found that I could replace it with-include, because it was normal that I didn't find it at the beginning. After the replacement, no error will be reported if it is not found.
So I want to see if the subdirectories are correct? Put echomsg. c and echomsg. h In the dir directory and find that the sed command is incorrectly executed. How can this problem be solved? Can't you think of info make as well. Is there a problem with-include. So I added a TAB before the include, and everything was normal. When the program came out, I lost the. d file and thought it was automatically deleted. I am very happy. It turns out that-include should be used in this way. However, when you use make-d to view debugging information, you find that the. d file is not generated at all. This indicates that this pattern rule does not work at all.
Later, we finally found that we should not add a TAB before the include command. Otherwise, the include command is considered a shell command and does not work at all !!! It seems that you should read more manuals. Let the include take effect again. Of course, the sed command is wrong, and then we can remember that because the sub-directory contains "/", and the s command of sed uses/as the separator, the sed command does not know what to do. In vim, you can replace/with + as the separator, so try again immediately and execute:
Echo dir/echomsg. o: dir/echomsg. c | sed's ++ \ ($ *)\. o [:] * + \ 1.o \ 1.d: dir/echomsg. c + G'
The result is displayed correctly, so it is placed in makefile, make, and found. d file is generated, but it does not contain main. o main. d: main. c. o: main. c, it seems that the sed command has not been executed at all, but is directly placed after the dependencies are generated by the compiler. d file, but the sed command has actually been executed (through make-d ). I cannot think of this problem. So I checked the information and decided to try the other two methods.
3. depend file and-MD method
The so-called depend method uses $ (CC)-MM $ (CPPFLAGS) $ ^> depend to put the dependency in the depend file, and then include depend. Check the debugging information through make-d and find that the method is updated. the depend file is automatically regenerated when the c file is updated. h, depend is not automatically regenerated. I don't know if this will affect the correctness. At the same time, make depend is required before make. It doesn't seem very good. In addition, all source files must be scanned when depend is automatically regenerated. Info make says this method is out of date.
-The MD method uses the compiler parameter-MD instead of-M to automatically generate the. d file by the compiler. It can be defined using the following pattern rules.
%. D: %. c
$ (CC) $ (CFLAGS)-MD-E $ <
Here,-E indicates that the compiler only processes the preprocessing phase without continuing to compile and link. Otherwise, a link error is reported. The. d file generated by this method does not include content such as main. o main. d: main. c, but only content such as main. o: main. c. Therefore, when the. c file is updated, the. d file is automatically regenerated, but when the. h file is updated,. d is not automatically regenerated. However, since each. c file corresponds to a. d file, only the changed source file is scanned to generate the corresponding. d file.
4. Modify the sed Method
When the preceding sed method cannot achieve the goal, the purpose of the sed command is to generate main. o main. d: main. c, so consider whether there are other methods to achieve the goal. Because $ * contains the directory part, it will have/. Therefore, consider whether you can only use the file name part. Actually, it should be okay, because the dependencies generated by the compiler are ,. o The file itself only contains the file name. As for the future compilation process, the compiler can use-I to specify the search path to find the corresponding file. After reading info make again, we found that $ (* F) can be used instead of $ *, thus solving the problem. The following is a makefile file for testing the above method. It is not well written, but can achieve the purpose of the test :-)
So how is the. d file re-generated? When you view debugging information through make-d, you can understand the makefile processing process and understand that the so-called makefile is always automatically regenerated. For makefiles that are initially executed and makefiles that are executed using the $ (MAKE)-C method, dependencies are always processed (dependency graphs are generated) it is re-executed (this is the two-phase process of make). During the second execution, the target part is executed. If the makefile file included in the target part exists, it first reads its content, and then searches for whether there are rules to update this file. If yes, and its dependency is new to this makefile, it will be regenerated and then include again. Therefore, when the aa. d file is included
Aa. o aa. d: aa. c \
/Root/Projects/test_makefile/bb. h \....
Therefore, when any one of the dependent files of the corresponding. o file is updated, the. d file is updated and re-include. Therefore, the dependency is also rebuilt.
5. Summary
After several days, I finally had some knowledge about the pattern rules provided by the implementation of automatic dependency in info make, but some problems are still not very clear.
A: It seems that the depend method corresponds to the generated one. o files are not automatically deleted because they are not intermediate files. The sed method makes them obsolete, and whether the sublines refer. the d file will be automatically deleted. I haven't deleted the test result, and it seems that I should not delete it, or I have to completely regenerate it. Isn't it the same effect as that after make clean!
B: Before $ (* F) is used to replace $ *, = and is used as the Separator in the s command of sed, which can be used in shell testing, why can't the same effect be achieved in makefile?
C: About. d file. h-file dependency when any. when the H file is changed, it depends on it. d files are updated. What is the purpose of this update? Is it necessary?
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service