使用MAKEFILE在编译之前复制文件并在之后删除它们

问题描述 投票:-2回答:2

我正在尝试复制文件befoe编译(我有两个同名的源文件,所以我将文件复制到具有不同名称的文件)并在MAKEFILE的末尾删除它们。我正在努力做到这一点,但可能在执行顺序中存在不匹配。我该怎么做才能正确?

all: copy_dup_files $(dst_dir) $(APP_TARGET_LIB) delete_dup_files

copy_dup_files:  
    @echo "COPYING DUP FILES"
    $(shell cp /aaa/hmac.c /aaa/hmac1.c )
    $(shell cp /bbb/hmac.c /bbb/hmac2.c )

delete_dup_files:
    @echo "DELETING DUP FILES"
    $(shell rm /aaa/hmac1.c )
    $(shell rm /bbb/hmac2.c )

谢谢

c makefile gnu-make
2个回答
2
投票

$(shell)的目的是产生一个输出,使得读取。配方行根本不应该有这个结构。

# this is evaluated when the Makefile is read
value := $(shell echo "Use the shell to produce a value for a variable")

# this is evaluated when you say "make foo"
foo:
    echo 'No $$(shell ...) stuff here'

因此,在读取$(shell ...)时,但在执行任何实际目标之前,会对您尝试中的所有Makefile内容进行评估。


0
投票

你的makefile试图说/aaa/hmac1.c依赖于/aaa/hmac.c。因此我们有:

/aaa/hmac1.c: /aaa/hmac.c
    cp $< $@

/bbb/hmac2.c: /bbb/hmac.c
    cp $< $@

/aaa/hmac1.o /bbb/hmac2.o: %.o: %.c
    gcc $< -o $@

myprog: /aaa/hmac1.o /bbb/hmac2.o
    gcc $^ -o $@

这是干净且并行安全的(对任何makefile的一个很好的测试)。

你可以做出无数的风格改进,比如

  • 摆脱绝对的道路
  • 使用符号链接而不是复制
  • 自动依赖关系生成(用于.h文件等)
  • 不要污染源树 - 将所有中间文件(.os和临时.cs)放在他们自己的构建文件夹中

&C。 &C。

© www.soinside.com 2019 - 2024. All rights reserved.