Makefile - 将多个文件夹中的头文件复制到一个包含依赖项的包含文件夹中

发布于 2024-10-07 18:33:38 字数 1442 浏览 0 评论 0原文

我在不同的文件夹中有多个文件,需要将它们复制到一个包含文件夹中:

make 之前

../security/security_features.h
../this/this_features.h
    [etc]
../that/that_features.h

make 之后

./include/security_features.h
./include/this_features.h
    [etc]
./include/that_features.h

./include 文件夹中的文件都需要依赖于各个文件夹中的文件。

Makefile 对我来说有点神秘,我尝试阅读手册并了解如何建立依赖关系:

./include/security_features.h: ../security/security_features.h
    @cp -av $< $@

但我必须对我的所有文件执行此操作 - 还有另一种更优雅的方法吗?

理想情况下,我只想编写分散文件夹中各种文件的 Makefile 变量,并且 makefile 知道将该变量中的所有文件复制到包含文件夹中。

有没有 Makefile 专家?

谢谢,

马特

更新:

我已经成功地完成了这样的文件列表:

INCLUDES_SRC := ../security/security_features.h ../this/this_features.h
INCLUDES_DST := $(addprefix ./include/,$(notdir $(INCLUDES_SRC)))

并且可以使一个依赖于另一个:

$(INCLUDES_DST): $(INCLUDES_SRC)
    @cp -av $< $@

但是,这只是复制第一个先决条件(../security/security_features.h)两次到两个目标(./include/security_features.h./include/this_features.h):

'../security/security_features.h' -> './include/security_features.h'
'../security/security_features.h' -> './include/this_features.h'

所以我现在有一个 src 和destination 的 make 变量列表文件位置 - 有人知道如何依次复制所有文件吗?

谢谢马特

I have multiple files in various folders that I need to copy to one include folder:

Before make

../security/security_features.h
../this/this_features.h
    [etc]
../that/that_features.h

After make

./include/security_features.h
./include/this_features.h
    [etc]
./include/that_features.h

The files in the ./include folder all need to be dependent on the files in the various folders.

Makefiles are a bit of a mystery to me, I have tried to read the manuals and understand how to make dependencies:

./include/security_features.h: ../security/security_features.h
    @cp -av 
lt; $@

But I'll have to do this for all my files - is there another more elegant way?

Ideally I'd just like to write a Makefile variable of the various files in the scattered folders and the makefile know to copy all the files in that variable to the include folder.

Any Makefile gurus out there?

Thanks,

Matt

UPDATE:

I have managed to do the list of files as such:

INCLUDES_SRC := ../security/security_features.h ../this/this_features.h
INCLUDES_DST := $(addprefix ./include/,$(notdir $(INCLUDES_SRC)))

And can make one dependent on the other:

$(INCLUDES_DST): $(INCLUDES_SRC)
    @cp -av 
lt; $@

However this just copying the first prerequisite (../security/security_features.h) both times to the two targets (./include/security_features.h, ./include/this_features.h):

'../security/security_features.h' -> './include/security_features.h'
'../security/security_features.h' -> './include/this_features.h'

So I now have a make variable list of src and destination locations for files - anyone know how to copy all the files in turn?

Thanks

Matt

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

戈亓 2024-10-14 18:33:38

您的规则的问题

$(INCLUDES_DST): $(INCLUDES_SRC)
    @cp -av 
lt; $@

在于:它使每个源标头成为每个目标标头的先决条件,并且仅复制第一个标头(使用 $<)。

这样就可以了:

.PHONY: INCLUDES
INCLUDES: $(INCLUDES_DST)

include/%: %
        cp $^ include

vpath %.h security this

粗略翻译,这意味着“制作 include/something,找到 something 并将其复制到 include/,当查找时对于 xh 形式的文件,请查看 security/this/。”

(请注意,如果您有两个同名的标头,则会错过其中一个,但在这种情况下,无论如何您都无法将它们都放入 include 中。)

如果您有很多源目录如果您不想将它们全部输入,Make 可以跟踪它们:

INCLUDE_PATHS := $(dir $(INCLUDES_SRC))

vpath %.h $(INCLUDE_PATHS)

The trouble with your rule:

$(INCLUDES_DST): $(INCLUDES_SRC)
    @cp -av 
lt; $@

is that it makes every source header a prerequisite of every destination header, and copies only the first one (with $<).

This will do it:

.PHONY: INCLUDES
INCLUDES: $(INCLUDES_DST)

include/%: %
        cp $^ include

vpath %.h security this

Roughly translated, this means "to make include/something, find something and copy it to include/, and when looking for a file of the form x.h, look in security/ and this/."

(Note that if you have two headers with the same name this will miss one of them, but in that case you couldn't get them both into include anyway.)

If you have a lot of source directories and you don't want to type them all in, Make can keep track of them:

INCLUDE_PATHS := $(dir $(INCLUDES_SRC))

vpath %.h $(INCLUDE_PATHS)
帥小哥 2024-10-14 18:33:38

这是一个棘手的问题。我发现的最佳解决方案是这样的:

FEATURES = a b c
HEADERS = $(foreach ftr,$(FEATURES),include/$(ftr)_feature.h)

default: $(HEADERS)
        @echo "Headers: $^"

$(HEADERS): include/%_feature.h: %

$(FEATURES):
        @echo "Copying $(subst DUMMY,$@,'../DUMMY/DUMMY_feature.h') to include/$@_feature.h"
        @cp $(subst DUMMY,$@,../DUMMY/DUMMY_feature.h) include/$@_feature.h

.PHONY: $(FEATURES)

更新:也许我应该解释一下这里发生的情况:

  1. 第一行​​定义了所有功能,即安全这个那个。我简单地称其为a、b和c。
  2. 标头名称源自您的功能名称。例如功能a成为标题include/a_feature.h
  3. 默认目标需要所有头文件
  4. $(HEADERS): include/%_feature.h: % 行让所有头文件依赖于它们的基本功能名称,即它替换 *include/SOMETHING_feature.h*与一些东西
  5. 最后一个目标将头文件从该位置的目录复制到项目内的目标目录。

不幸的是,它不检查 ../FEATURE 目录中头文件的时间戳。

干杯,

马丁。

that's a tricky one. Best solution I found is this:

FEATURES = a b c
HEADERS = $(foreach ftr,$(FEATURES),include/$(ftr)_feature.h)

default: $(HEADERS)
        @echo "Headers: $^"

$(HEADERS): include/%_feature.h: %

$(FEATURES):
        @echo "Copying $(subst DUMMY,$@,'../DUMMY/DUMMY_feature.h') to include/$@_feature.h"
        @cp $(subst DUMMY,$@,../DUMMY/DUMMY_feature.h) include/$@_feature.h

.PHONY: $(FEATURES)

Update: Maybe I should explain a little bit what happens here:

  1. The first line defines all your features, that is security, this and that. I simpy called tham a, b and c.
  2. The header names are derived from your feature names. E. g. feature a becomes header include/a_feature.h.
  3. The default target requires all the headers
  4. The line $(HEADERS): include/%_feature.h: % lets all header files depend on their base feature names, i. e. it replaces *include/SOMETHING_feature.h* with SOMETHING.
  5. The last target copies the header files from the directory of the location to the target directory within the project.

Unfortunately, it doesn't check timestamps of the header files in the ../FEATURE directory.

Cheers,

Martin.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文