导致cmake强制重新编译文件,但前提是项目被重新编译?
我通常有和可以cmake can cmake can始终强制汇编/构建特定文件?
我使用
__ Date __
有一个C ++文件来显示我的应用程序的构建日期。但是,如果未修改此文件,则不会重建该文件,并且不会更新日期。cmake可以始终重建该特定文件吗?
...除了我想要一些稍有不同的东西:
在CMAKE项目中,我(对于C,用于使用我使用的transpiles),有时我运行make
时,代码没有实际更改从程序没有重新编译(或重新链接)的意义上,可以很好地检测到。
显然,在这种情况下,我不想更新时间戳,最终得到了一个新的可执行文件,除了构建日期外,它与前面相同。
我在引用的帖子中看到,只需确保文件上更改的时间戳即可迫使重新编译。因此,假设我的__ DATE __
用法在use_date.c
中,我想要的是use_date.date.c
的时间戳已更新(已更新(强迫重新编译),只有在项目中的任何其他文件(例如main.c
)进行了更改,因此它会迫使项目重新编译和链接(显然,如果我只是只有更改use_date.c
手动,没有其他文件)。
因此,假设我的项目只是生成可执行文件(没有库):
add_executable(my_project use_date.c other_file.c main.c)
... ...是否可以添加一个cmake步骤,以更新use_date.c
的时间戳(从而导致其重新编译),仅导致其重新编译)如果否则,该项目将重新编译和重新链接?
I have generally the same question as in Can CMake always force the compilation/build of a specific file?
I have a C++ file using
__DATE__
to display the build date of my app. But if this file is not modified, it will not be rebuilt and the date will not be updated.Can CMake always rebuild that specific file?
... except I want something slightly different:
In the CMake project I have (for C, transpiles to Makefile which I use), sometimes there are no actual changes to the code when I run make
, which is detected nicely, in the sense that there is no recompilation (or relinking) of the program.
Obviously, in this case, I do not want to update the timestamp, and end up with a new executable, which is otherwise identical to the previous one - apart from the build date.
I have seen in the quoted post, that one simply has to ensure a changed timestamp on the file, to force a recompilation. So, assuming my __DATE__
usage is in use_date.c
, what I'd want, is that the timestamp of use_date.c
is updated (forcing recompilation), only if any other file in the project (say, main.c
) has been changed, so it forces project recompilation and linking (obviously, this should also work if I just change use_date.c
manually, and no other file).
So, assuming my project just generates an executable (no libraries):
add_executable(my_project use_date.c other_file.c main.c)
... is it possible to add a CMake step, that updates the timestamp of use_date.c
(and thus causes its recompilation), only if otherwise the project is getting recompiled and relinked?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,找到了一种方法:这似乎是一种吱吱作响的解决方案 - 希望在CMAKE中更有知识的人最终会发布适当的解决方案。但是与此同时:
我发现
add_custom_command
带有POST_BUILD
运行custic命令仅如果新二进制(.elf <
) /code>对我来说)是生成的,否则不是运行自定义命令。
因此,基本上,我们可以在自定义命令中有一个小的bash脚本:
.felf
(出于其他原因)已建立touch
use_date.c
,而在bash
脚本中,请逐渐降为构建目录,并明确跑步再次制作
- 它应该仅重新编译use_date.c
,然后与上一个构建中的其余链接。这本质上是有效的 - 除了它导致无限循环。
这可以通过创建一个临时文件来解决:
.felf
(出于其他原因)touch
use_date.c
,而在bash
script中仍然,下降到构建目录,然后再次显式运行make
- 应该....feld。临时文件,然后退出 - 停止无限循环
这似乎可以正常工作:
OK, found a way: it seems kind of a squeaky solution - hopefully someone more knowledgeable in CMake will post a proper solution eventually. But in the meantime:
I've found that
add_custom_command
withPOST_BUILD
runs the custom command only if a new binary (.elf
for me) is generate, and otherwise does not run the custom command.So, basically, we could have a small bash script in the custom command:
.elf
(for whatever other reasons) has been builttouch
theuse_date.c
, and while in thebash
script still, descend into the build directory, and explicitly runmake
again - which should recompile onlyuse_date.c
, and then link with the rest from the previous build.This essentially works - except, it causes an infinite loop.
This can get solved with creating a temp file:
.elf
(for whatever other reasons) has been builttouch
theuse_date.c
, and while in thebash
script still, descend into the build directory, and explicitly runmake
again - which should ....elf.
build (for the toucheduse_date.c
), so we want to remove the temp file, and just exit - stopping the infinite loopThis seems to work fine: