当Makefile不存在或目录已被修改时,它将在另一个目录中编译一个库 [英] Makefile compile a library in another directory when it doesn't exist or the directory has been modify

查看:100
本文介绍了当Makefile不存在或目录已被修改时,它将在另一个目录中编译一个库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个makefile,可以编译我的库,然后编译程序.我想做的是重新生成makefile,以便始终为此修改我的库文件.

Hi i have a makefile that compiles my library and then compiles the program. What i want to do is that the makefile recompile alway i modify my library's files for that i thought in this

ifneq ("$(wildcard $(PATH_LIB.A)","")
FILE_EXIST = 1
else
FILE_EXIST = 0
endif

$(MAIN_PROCESS): $(PATH_LIB.A) check_lib
...thing to do...

$(PATH_LIB.a):
FILE_EXIST = 0

check_lib:
ifeq("$(FILE_EXIST)","0")
    $(MAKE) -C $(PATH_MAKEFILE_LIB.A)
endif

我的问题是,当我进行编译时,它始终重新链接"...要做的事情...",因为正在检查所有时间的check_lib是否可更新,您建议做什么?

My problem es that when i compile it relinks all time "...thins to do..." because is checking all time check_lib as updateable what do you suggest for do what i want to do?

推荐答案

Make不是bash或python之类的脚本语言.它需要的是对目标和先决条件之间相互依赖关系的描述,以及建立它们的秘诀.对于您的情况(但我不确定我是否了解所有详细信息),您可以尝试:

Make is not a scripting language like bash or python. What it needs is a description of inter-dependencies between targets and prerequisites, plus recipes to build them. In your case (but I am not sure I understood all details) you could try:

$(MAIN_PROCESS): $(PATH_LIB.A)
    ...thing to do...

$(PATH_LIB.A):
    $(MAKE) -C $(PATH_MAKEFILE_LIB.A)

仅此而已(但请继续阅读,还有更多要了解的内容).这说明:

And that's all (but continue reading, there is more to understand). This tells make that:

  1. $(MAIN_PROCESS)取决于$(PATH_LIB.A),此外,如果$(MAIN_PROCESS)不存在或早于$(PATH_LIB.A),则需要执行构建c $(MAIN_PROCESS)的事情.
  2. $(PATH_LIB.A)不依赖任何内容,如果不存在该怎么办.
  1. $(MAIN_PROCESS) depends on $(PATH_LIB.A), plus the things to do to build $(MAIN_PROCESS) if it does not exist or if it is older than $(PATH_LIB.A).
  2. $(PATH_LIB.A) depends on nothing, plus what to do if it does not exist.

几乎可以使用.几乎仅因为如果$(PATH_LIB.A)已经存在但已过期(相对于其自身的源文件),则不会重建它.一种解决方案是将其声明为 phony :

It almost works. Almost only because if $(PATH_LIB.A) already exists but is out of date (with respect to its own source files) it will not be rebuilt. A solution is to declare it as phony:

.PHONY: $(PATH_LIB.A)

$(MAIN_PROCESS): $(PATH_LIB.A)
    ...thing to do...

$(PATH_LIB.A):
    $(MAKE) -C $(PATH_MAKEFILE_LIB.A)

通过这种方式,make将始终尝试重建它,即使它已经存在.子品牌将在需要时执行此操作,否则只会告诉您它是最新的.但这不是全部内容:由于make总是尝试重建$(PATH_LIB.A),因此即使子make因为$(PATH_LIB.A)处于最新状态而未执行任何操作,它也会认为也必须重建$(MAIN_PROCESS).日期.

This way make will always try to rebuild it, even if it already exists. The sub-make will do it if needed, else it will just tell you that it was up to date. But it is not the whole story: as make always tries to rebuild $(PATH_LIB.A), it will consider that $(MAIN_PROCESS) must also be rebuilt, even if the sub-make didn't do anything because $(PATH_LIB.A) was up-to-date.

如果这是一个问题,可以使用更棘手的解决方案,例如使用一个以上的子品牌.这个想法是这样的:

If this is a problem, more tricky solutions can be used, like using one more sub-make. The idea is the following:

  1. 使用make conditional为您的$(MAIN_PROCESS)目标使用两个不同的规则创建两个不同的调用上下文.
  2. 在第一次调用make时,会使用第一个上下文,其中$(MAIN_PROCESS)取决于 phony $(PATH_LIB.A),但是在第二个调用中,其配方(而不是...thing to do...)是make的第二次调用其他背景.
  3. 对于第二次调用,$(MAIN_PROCESS)取决于非语音 $(PATH_LIB.A),并且具有其正常配方.
  1. Use make conditionals to create two different contexts of invocation with two different rules for your $(MAIN_PROCESS) target.
  2. On the first invocation of make, the first context is used where $(MAIN_PROCESS) depends on the phony $(PATH_LIB.A) but its recipe, instead of ...thing to do... is a second invocation of make, in the other context.
  3. For this second invocation $(MAIN_PROCESS) depends on the non-phony $(PATH_LIB.A) and will have its normal recipe.

由于使用了专用的make变量(以下代码中的SECONDPASS),因此区分了这两种上下文.

The two contexts are distinguished thanks to a dedicated make variable (SECONDPASS in the code below).

示例:

host> cat lib/Makefile
foo.a: foo.c
    touch $@

host> cat Makefile
ifeq ($(SECONDPASS),)
$(MAIN_PROCESS): $(PATH_LIB.A)
    $(MAKE) SECONDPASS=1

.PHONY: $(PATH_LIB.A)

$(PATH_LIB.A):
    $(MAKE) -C $(dir $@)
else
$(MAIN_PROCESS): $(PATH_LIB.A)
    touch $@
endif

host> make --no-print-directory
make -C lib/
touch foo.a
make SECONDPASS=1
touch bar

host> make --no-print-directory
make[1]: 'foo.a' is up to date.
make SECONDPASS=1
make[1]: 'bar' is up to date.

host> touch lib/foo.c

host> make --no-print-directory
make -C lib/
touch foo.a
make SECONDPASS=1
touch bar

host> touch lib/foo.a

host> make --no-print-directory
make -C lib/
make[1]: 'foo.a' is up to date.
make SECONDPASS=1
touch bar

这篇关于当Makefile不存在或目录已被修改时,它将在另一个目录中编译一个库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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