当前位置:网站首页>The role of include in makefile

The role of include in makefile

2022-07-23 09:08:00 Biao

When we compile , I hope it has been changed c file , Just compile that one c file , meanwhile , When changing a header file , You also want to include this header file c Files are also compiled . For the former , It's simple ,make The default is to do this , And for the latter , But it needs our cooperation gcc Some options to achieve this . therefore include The function of is to improve the compilation speed .

( Of course not , We are makefile Write in xx.o:xx.c 1.h 2.h 3.h ... Can also be realized , Just tell make, My every .c Rely on all .h, This can also achieve any .h Revised , Will rewrite the compilation .o, But this obviously causes a lot of unnecessary compilation , The performance drops )

If you know now .c And those .h Our dependency , Store in a .d File can ( Obviously , How many .c, How many corresponding .d file ). then makefile Add a sentence to the list :

include xx.d

make Software is executing makefile Before the first goal in ( It's usually all The goal is ), Meeting Take this xx.d File included for use , can To guide the make The compilation process of the software , Reduce unnecessary compilation .

And how to know one c Which header files are included in the file Well ,make Software is unknown , Then what shall I do? , Compiler it knows ah , So let the compiler generate this dependency .d file . Compilers such as gcc by .c File generation .d file , Add an option ,gcc -MMD

But there is a problem here ,make For the first time makefile When ,.d The file doesn't exist yet , because gcc I haven't compiled it yet . At this time  include xx.d It'll be wrong , such as :makefile:6: xxx.d: No such file or dirctory.

Of course, we can also give this  xx.d Write a generation rule , such as :

xx.d:

   @echo "-------- [email protected] ---------"

In this case , Will be the default in makefile In order to find xxx.d Target rules and execute , If the rule is not found , An error will be reported and the execution will be suspended .make: *** No rule to make target `tmp.d'. Stop.

therefore make Handle include tmp.d When , The execution process is as follows :

1. Check makefile Whether there is tmp.d file , If you find , End and apply tmp.d file , Otherwise, a warning will be given .

2. lookup makefile Is there any in the document with tmp.d Rules for the goal , If you find , End and apply tmp.d file , Otherwise, an error will be reported .

Because of our xx.d Files depend on compiler generated , So for the first time make, This file is not available , So it will report a mistake , And don't write tmp.d Target rules , And directly make Execution stop . We just need to write as

-include xx.d, So that we can Give Way make Don't make a mistake , You can continue to run . First compilation , It doesn't matter if you don't rely on files , Because it will be all anyway .c All need to be compiled , Well .

although make Each time, it is generated by the last compilation .d file , But it doesn't matter , Because a .c Rely on the .h The file has changed , It will be recompiled , And this .c This header file is not included , Belong to .c The contents of the document have changed , It will be recompiled , therefore , This dependency is generated by the last compilation .d file , Is safe , Correct .

Reference article :

Makefile How to automatically generate header file dependencies _panfei263031 The blog of -CSDN Blog _makefile The header file depends on

Makefile Medium include ( Dependency file ) Implementation process of _Jerry.yl The blog of -CSDN Blog _makefile Dependency file

Makefile in include Have to use _qq_542865340 The blog of -CSDN Blog _makefile add to include

Makefile Automatic generation of header file depends on _Andy Pines The blog of -CSDN Blog _makefile The header file depends on

 

原网站

版权声明
本文为[Biao]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207230038218297.html