简体   繁体   English

如何使用makefile生成自动头文件

[英]How to generate auto header file with using makefile

I am working on a project which has a large source code. 我正在开发一个包含大量源代码的项目。 I want to create a header file which includes specific environment variables using makefile while the project is being compiled because the project has different types. 我想在编译项目时使用makefile创建一个包含特定环境变量的头文件,因为该项目具有不同的类型。 I want to use header file anywhere in the project because I don't want to include some code snippets or vice versa while project is being compiled. 我想在项目中的任何地方使用头文件,因为在编译项目时,我不想包含一些代码片段,反之亦然。 How can I do it? 我该怎么做?

Here's a quick solution. 这是一个快速的解决方案。 The idea is to generate a temporary .h file, and only update the actual .h if the something has changed. 这个想法是生成一个临时的.h文件,并且只有在某些内容发生变化时才更新实际的.h文件。 This prevents you from rebuilding every time. 这样可以防止您每次都进行重建。

 #dummy target that forces another to be run once per make invokation
 .FORCE:

 #target is run once per make
 file.h.gen: .FORCE
      echo "FOO=$(FOO)" > $@
      echo "BAR=$(BAR)" > $@

 #because file.h.gen is always updated, this is run once per make
 file.h : file.h.gen
      rsync --checksum $< $@

Note that if you modify any variable in file.h, it will need to rebuild all the files that depend on it. 请注意,如果您修改file.h中的任何变量,它将需要重建依赖于它的所有文件。 If you want, you can break the .h file into multiple files to give better granularity. 如果需要,可以将.h文件分成多个文件,以提供更好的粒度。 For example, you could create a header file per variable, and each source file would only include the variable headers it's interested in. This way you only rebuild the files you need. 例如,您可以为每个变量创建一个头文件,并且每个源文件将仅包含其感兴趣的变量头。这样,您仅重建所需的文件。

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

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