Makefile generator creates two files

前端 未结 2 1729
一整个雨季
一整个雨季 2021-01-23 04:53

I have a generator program that creates two version files, say ver.h and ver.cpp. My ultimate build target depends on both of these files, and the rule

2条回答
  •  暖寄归人
    2021-01-23 05:39

    Using the phony target as the prerequisite is a bad idea. program will be run even if ver.* files exist, which is a false positive error.

    More subtly, GNU Make is only guaranteed to update its file timestamp, if that file is a target of a rule with a recipe. So here, even though program is always run, anything that in turn depends on ver.* files might not get updated at all!

    In my opinion it is best to not make up unnatural patterns for each target, but instead, go explicit:

    There is a "main" file that you are generating, that is ver.cpp. Use the "no-op" recipe ; for the other one, which can be put on the same line like this:

    ver.h: ver.cpp ;
    ver.cpp: Makefile
        ./gen/version/program
    

    This method starts with what you wrote, but adds the very important ;.

    If you did not have a natural candidate for the "main" file, then in my opinion it is best to use a "sentinel":

    ver.h ver.cpp: sentinel ;
    
    sentinel: Makefile
        ./gen/version/program
        touch $@
    

    Again, this method is similar to one of your methods, but very importantly, does not use a phony, but a real file.

提交回复
热议问题