如何确保在makefile中的所有其他构建规则之前运行目标? [英] How to ensure a target is run before all the other build rules in a makefile?

查看:92
本文介绍了如何确保在makefile中的所有其他构建规则之前运行目标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C ++项目,其中包含一个所有其他C ++文件都依赖的生成文件.我试图在开始任何其他编译之前强制生成和编译该文件.通常,这就像将文件首先放在all:目标中那样简单,但是复杂的是我的Makefile也是由构建系统生成的,我只能将片段附加到Makefile中,而不是通常对其进行编辑.

I have a C++ project which contains a generated file that all the other C++ files depend on. I'm trying to force that file to be generated and compiled before any other compilation begins. Usually it'd be as simple as putting that file first in the all: target, but the complication is that my Makefile is also generated by a build system, and I can only append fragments to the Makefile, not edit it in general.

那么,是否有一种方法可以通过依赖关系或其他方式强制生成的文件目标首先运行?我想过要使用这样的东西:

So, is there a way to force that generated file target to run first, via dependencies or otherwise? I've thought of using something like this:

cpp_files := $(wildcard src/*.cpp)
$(cpp_files): generated_file.cpp
generated_file.cpp:
    # generate the file here

但是它似乎不起作用.作为参考,我的源目录结构是这样的,因此我需要递归收集cpp文件:

But it doesn't seem to work. For reference, my source dir structure is like this, so I need to collect the cpp files recursively:

src/
|---file1.cpp
|---file2.cpp
|---subdir1/
    |---file3.cpp

gen/
|---generated_file.cpp

推荐答案

如果您确定这确实是您想要的,这是一种实现方法:制定一个虚拟文件规则,makefile将对该文件进行include处理.

If you're sure that's really what you want, here's one way to do it: have a rule for a dummy file which the makefile will include.

.PHONY: dummy
dummy:
    # generate the file here

-include dummy

无论您指定什么目标,Make都会先运行dummy规则,然后重新启动.

No matter what target you specify, Make will first run the dummy rule, then restart itself.

这篇关于如何确保在makefile中的所有其他构建规则之前运行目标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆