如何使用make转换子目录中的文件? [英] How to use make to convert files in subdirectories?

查看:55
本文介绍了如何使用make转换子目录中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有一堆子目录的目录.每个子目录都包含一个traj.dat文件.我想使用一个Makefile来确保将文件traj.dat转换为其他格式,并且将输出文件打印在与原始文件相同的子目录中.

I have a directory with a bunch of subdirectories. Each subdirectory contains a traj.dat file. I want to use a Makefile to make sure that the file traj.dat gets converted to a different format, and the output file is printed in the same subdirectory as the original file.

因此,如果我想指定子目录的名称,则可以使用:

Therefore, if I wanted to specify the names of the subdirectories, I could just use:

subdir1/traj.dat.xyz: subdir1/traj.dat
    my_convert subdir1/traj.dat subdir1/traj.dat.xyz
subdir2/traj.dat.xyz: subdir2/traj.dat
    my_convert subdir2/traj.dat subdir2/traj.dat.xyz

等等.

如何获得包含traj.dat文件的所有子目录的以上结果,而不管它们的名称如何,而不必显式列出它们?

How can I get the above result FOR ALL the subdirectories containing a traj.dat file, regardless of their name, without having to list them explicitly?

干杯!

推荐答案

假设您使用的是GNU make,请尝试:

Assuming you're using GNU make, try:

DATFILES := $(shell find . -name traj.dat)

OUTFILES := $(addsuffix .xyz,$(DATFILES))

all: $(OUTFILES)

%.dat.xyz : %.dat
        my_convert $< $@

您并不太清楚子目录"的含义;如果您只是想表示直接子目录,则可以使用此函数代替 shell 函数,该函数效率更高(并且适用于Windows):

You weren't really clear what you mean by "subdirectories"; if you just mean immediate subdirectories you can use this instead of the shell function, which is more efficient (and works on Windows):

DATFILES := $(wildcard */traj.dat)

这篇关于如何使用make转换子目录中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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