简体   繁体   English

自动依赖生成

[英]Automatic Dependency Generation

I was reading through Automatic Dependency Generation in the make manual and I can't figure out why this functionality is needed. 我正在阅读制作手册中的“ 自动依赖项生成” ,但不知道为什么需要此功能。 The project that I am working on and I started writing from scratch is structured like this: 我正在研究并从头开始编写的项目的结构如下:

  • each unit (library) has it's own folder 每个单元(库)都有自己的文件夹
  • each .cpp file should include a single .h file with the same name from the same directory. 每个.cpp文件应包括一个来自同一目录的同名.h文件。 OK, this rule can be a bit too restrictive, like in the case of circular dependencies, in which case I may include another .h file, just as described in the next rule 好的,此规则可能有点过于严格,例如在循环依赖的情况下,在这种情况下,我可能会包含另一个.h文件,就像下一条规则中所述
  • when including other headers in a .h file, always use paths relative to the root directory of the project, if the dependency is located in another unit (library). 当在.h文件中包含其他头文件时,如果依赖项位于另一个单元(库)中,则始终使用相对于项目根目录的路径。 Otherwise, just include the name of the file. 否则,只需添加文件名即可。

In the makefile, I pass -I . 在生成文件中,我传递-I . to the compiler. 到编译器。 When it encounters a .cpp file in directory X, it will search for the .h file in the same directory (or in the . directory). 当它在目录X中遇到.cpp文件时,它将在同一目录(或.目录)中搜索.h文件。 When parsing the .h file, it will encounter includes relative to the . 解析.h文件时,它将遇到相对于的包含. folder, so it will know where to look for them. 文件夹,因此它将知道在哪里寻找它们。

Now, why would anybody want to generate a list of dependencies with the -M flag and mess with sed to produce an obscure .d file (dependencies fie) if code can be structured like I described above? 现在,如果代码可以像我上面描述的那样结构化,为什么有人会生成带有-M标志的依赖项列表并与sed混淆以生成晦涩的.d文件(依赖项文件)? I don't see the point of generating the specific list of dependencies from a code file. 我看不出从代码文件生成依赖关系的特定列表的意义。

Because in practice, each source file will depend on multiple header files. 因为实际上,每个源文件都将依赖于多个头文件。 If you don't recompile the source file each time any of those headers changes, there is a good chance you will end up with an inconsistent binary. 如果您没有在每次这些标头更改时都重新编译源文件,则很有可能会导致二进制文件不一致。

That's just how makepp works. 这就是makepp的工作方式。 Dependencies are detected automatically. 依赖关系会自动检测。 In your case you won't even need a makefile (if you don't mind specifying the target in the command). 在您的情况下,您甚至不需要makefile(如果您不介意在命令中指定目标)。

The built in linker rule has the goody of automatically deducing object files. 内置链接器规则具有自动推导目标文件的优点。 If you say makepp proggie , it will scan proggie.c (or .cpp or whatever you have) for include statements. 如果您说makepp proggie ,它将扫描proggie.c(或.cpp或您拥有的任何文件)以查找包含语句。 For each statement it will look if there is a matching .o file that can be built, and if so scan that recursively. 对于每个语句,它将查看是否可以构建匹配的.o文件,如果是,则以递归方式对其进行扫描。 All the .o files discovered this way are then built and linked together. 然后,将以这种方式发现的所有.o文件构建并链接在一起。

Or your makefile could be a one liner to avoid specifying the target every time: 或者,您的makefile可能只是一个衬里,以避免每次都指定目标:

$(phony all): proggie

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM