Reputation: 421
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?
Upvotes: 0
Views: 1610
Reputation: 6377
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.
#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.
Upvotes: 1