GNU Make - 对非程序代码的依赖

发布于 2024-09-02 23:22:15 字数 681 浏览 7 评论 0原文

我正在编写的程序的一个要求是它必须能够信任配置文件。为了实现这一点,我使用多种哈希算法在编译时生成文件的哈希值,这会生成一个以哈希值作为常量的标头。

其依赖关系非常简单,我的程序依赖于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

救赎№ 2024-09-09 23:22:16

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

静谧幽蓝 2024-09-09 23:22:16

当您将 config/config_file.cfg 添加到 config_hash.h 的依赖项时发生了什么,为什么它不是您所期望的?

类似的规则

config_hash.h:config/config_file.cfg
    $(SH) genhash 
lt; > $@

如果 config/config_file.cfg 较新,则 将重新生成 config_hash.h。然后,您的 gcc 生成的依赖项将根据 config_hash.h 重新编译任何内容。

$@ 变量是目标,使用它可确保您正在创建所需的文件(在您的问题中,如果定义了 srcdir ,则规则表示它将生成 ./config_hash.h,但实际上会创建./$(srcdir)/config_hash.h)。类似地,$<$^ 分别给出第一个和所有先决条件。

我假设你有一个像这样的makefile

CPPFLAGS+=-MMD -MP
all:
# etc.
config_hash.h:config/config_file.cfg
    $(SH) genhash 
lt; > $@
%.d %.o:%.c
    $(CC) $(CPPFLAGS) $(CFLAGS) -c -o $*.o 
lt;
%.d %.o:%.cpp
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $*.o 
lt;
-include $(wildcard *.d) /dev/null

What happened when you added config/config_file.cfg to the dependancies of config_hash.h, and why wasn't it what you expected?

A rule like

config_hash.h:config/config_file.cfg
    $(SH) genhash 
lt; > $@

would regenerate config_hash.h if config/config_file.cfg was more recent. Your gcc generated dependancies would then recompile anything depending on config_hash.h.

The $@ variable is the target, using this ensures you are creating the file you asked for (In your question, if srcdir 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

CPPFLAGS+=-MMD -MP
all:
# etc.
config_hash.h:config/config_file.cfg
    $(SH) genhash 
lt; > $@
%.d %.o:%.c
    $(CC) $(CPPFLAGS) $(CFLAGS) -c -o $*.o 
lt;
%.d %.o:%.cpp
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $*.o 
lt;
-include $(wildcard *.d) /dev/null
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文