GNU Make - 对非程序代码的依赖
我正在编写的程序的一个要求是它必须能够信任配置文件。为了实现这一点,我使用多种哈希算法在编译时生成文件的哈希值,这会生成一个以哈希值作为常量的标头。
其依赖关系非常简单,我的程序依赖于 config_hash.h,它有一个生成它的目标。
makefile 看起来像这样:
config_hash.h:
$(SH) genhash config/config_file.cfg > $(srcdir)/config_hash.h
$(PROGRAM): config_hash.h $(PROGRAM_DEPS)
$(CC) ... ... ...
我正在使用 gcc 的 -M 选项,这对于处理依赖关系非常有用。如果我的标题发生变化,我的程序就会重建。
我的问题是,我需要能够判断配置文件是否已更改,以便重新生成 config_hash.h。我不太确定如何解释这种对 GNU make 的依赖。
我尝试将 config/config_file.cfg 列为 config_hash.h 的依赖项,并为 config_file.cfg 提供 .PHONY
目标,但没有成功。显然,我不能依赖 gcc 的 -M 开关来帮助我,因为配置文件不是任何目标代码的一部分。
有什么建议吗?不幸的是,我不能发布大部分 Makefile,否则我就会发布整个内容。
A requirement for a program I am writing is that it must be able to trust a configuration file. To accomplish this, I am using several kinds of hashing algorithms to generate a hash of the file at compile time, this produces a header with the hashes as constants.
Dependencies for this are pretty straight forward, my program depends on config_hash.h, which has a target that produces it.
The makefile looks something like this :
config_hash.h:
$(SH) genhash config/config_file.cfg > $(srcdir)/config_hash.h
$(PROGRAM): config_hash.h $(PROGRAM_DEPS)
$(CC) ... ... ...
I'm using the -M option to gcc, which is great for dealing with dependencies. If my header changes, my program is rebuilt.
My problem is, I need to be able to tell if the config file has changed, so that config_hash.h is re-generated. I'm not quite sure how explain that kind of dependency to GNU make.
I've tried listing config/config_file.cfg as a dependency for config_hash.h, and providing a .PHONY
target for config_file.cfg without success. Obviously, I can't rely on the -M switch to gcc to help me here, since the config file is not a part of any object code.
Any suggestions? Unfortunately, I can't post much of the Makefile, or I would have just posted the whole thing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在
.PHONY
中声明文件是错误的。那里列出的任何依赖项都不会在文件系统中进行检查。只需将其列为哈希标头的依赖项并从那里开始即可。Declaring the file in
.PHONY
is wrong. Any dependency listed there will not be checked in the filesystem. Just list it as a dependency for the hash header and go from there.当您将
config/config_file.cfg
添加到config_hash.h
的依赖项时发生了什么,为什么它不是您所期望的?类似的规则
如果
config/config_file.cfg
较新,则 将重新生成config_hash.h
。然后,您的 gcc 生成的依赖项将根据config_hash.h
重新编译任何内容。$@
变量是目标,使用它可确保您正在创建所需的文件(在您的问题中,如果定义了srcdir
,则规则表示它将生成./config_hash.h
,但实际上会创建./$(srcdir)/config_hash.h
)。类似地,$<
和$^
分别给出第一个和所有先决条件。我假设你有一个像这样的makefile
What happened when you added
config/config_file.cfg
to the dependancies ofconfig_hash.h
, and why wasn't it what you expected?A rule like
would regenerate
config_hash.h
ifconfig/config_file.cfg
was more recent. Your gcc generated dependancies would then recompile anything depending onconfig_hash.h
.The
$@
variable is the target, using this ensures you are creating the file you asked for (In your question, ifsrcdir
is defined the rule says it will generate./config_hash.h
, but will actually create./$(srcdir)/config_hash.h
). Similarly$<
and$^
give the first and all prerequisites respectively.I'm assuming you have a makefile like