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

时间:2022-03-11 19:27:06

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. 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?

我正在开发一个包含大量源代码的项目。我想在编译项目时使用makefile创建一个包含特定环境变量的头文件,因为项目有不同的类型。我想在项目的任何地方使用头文件,因为我不想在编译项目时包含一些代码片段,反之亦然。我该怎么做?

1 个解决方案

#1


0  

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. This prevents you from rebuilding every time.

这是一个快速的解决方案。我们的想法是生成一个临时的.h文件,只有在事情发生变化时才更新实际的.h文件。这可以防止您每次都重建。

 #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. If you want, you can break the .h file into multiple files to give better granularity. 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.

请注意,如果修改file.h中的任何变量,则需要重建所有依赖于它的文件。如果需要,可以将.h文件拆分为多个文件,以提供更好的粒度。例如,您可以为每个变量创建一个头文件,每个源文件只包含它感兴趣的变量头。这样您只需重建所需的文件。

#1


0  

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. This prevents you from rebuilding every time.

这是一个快速的解决方案。我们的想法是生成一个临时的.h文件,只有在事情发生变化时才更新实际的.h文件。这可以防止您每次都重建。

 #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. If you want, you can break the .h file into multiple files to give better granularity. 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.

请注意,如果修改file.h中的任何变量,则需要重建所有依赖于它的文件。如果需要,可以将.h文件拆分为多个文件,以提供更好的粒度。例如,您可以为每个变量创建一个头文件,每个源文件只包含它感兴趣的变量头。这样您只需重建所需的文件。