繁体   English   中英

c ++ makefile项目如何创建具有相同基本名加后缀和相同扩展名的文件列表

[英]c++ makefile project how can I create a list of files with same basename plus suffix and same extension

抱歉,如果标题误导我,我无法说清楚它的正确性。

基本上,我使用的是RTI DDS生成器来获取* .idl文件并生成* .cxx和* .h文件。

因此,生成工作正常,但是我试图添加一条规则来清理所有生成的文件。

从单个* .idl文件中,获得以下生成的文件:

在test.idl上运行rtiddsgenerate test.idl产生:

test.cxx
test.h
testPlugin.cxx
testPlugin.h
testSupport.cxx
testSupport.h

所以我需要一个干净的规则来删除这些。 我开始列出列表,这是我拥有的伪代码(未经测试,因为我必须将其手工复制到我的电子邮件PC上):

# Generate my lists of files
DDS_FILES = $(wildcard *.idl)
DDS_GEN_CXX = (DDS_FILES:.idl=.cxx)
DDS_GEN_H = (DDS_FILES:.idl=.h)

# Main rule - doesn't really do anything for the moment except call the DDS_GEN_CXX rule
run: $(DDS_GEN_CXX)

# rule for generating cxx files:
%.cxx: %.idl
    rtiddsgen.bat $<

# Rule to clean
clean:
    rm -f $(DDS_GEN_CXX) $(DDS_GEN_H)

因此,在这里您可以看到我已经为cxx和h文件制作了文件列表,但是它们只包含与.idl文件同名的文件(即test.cxx和test.h),而没有包含具有test.cxx的文件。和test.h-后缀为Plugin and Support

确实有两个问题:1.如何创建具有这些额外文件的更多变量,例如DDS_GEN_SUPPORT_CXX=? DDS_GEN_PLUGIN_CXX=? DDS_GEN_SUPPORT_H=? 等等。2.在make文件中是否有更好/更清晰/更有效的方法来执行相同的操作?

您可以使用$(shell ...)获取生成的文件。 要获取带有后缀插件和支持的文件,请执行以下操作

GEN_FILES=$(shell ls test* | grep -v '\.idl' | grep -E 'Plugin|Support')

你的干净规则看起来像

clean:
     rm $(GEN_FILES)

有点偏离主题,但是要使您的Makefile健壮,应使用具有多个输出的规则(仅适用于模式规则):

%.cxx %.h %Plugin.cxx %Plugin.h %Support.cxx %Support.h: %.idl
    rtiddsgen.bat $<

上面说rtiddsgen.bat从输入%.idl生成5个文件。

不知道您是否已经完全解决了这个问题,但是在一条评论中,您指出...

这些名称中可能有任意数量的任意名称(test1.idl,something.idl等),因此我无法使用此规则

您可能可以使用make的foreach内置函数解决此问题。

# Start with the list of .idl files in the current directory.
#
DDS_FILES := $(wildcard *.idl)

# Strip the .idl extension from the .idl files.
#
DDS_FILES_BASES := $(patsubst %.idl,%,$(DDS_FILES))

# Specify the list of suffixes that will be used by rtiddsgen.bat.  Depending on
# the specifics of the problem it may be desirable to split this into multiple
# variables: e.g. one for .h files and one for .cxx files.  The principle
# remains the same regardless.
#
DDS_FILES_SUFFIXES := .h .cxx Plugin.h Plugin.cxx Support.h Support.cxx

# idl_generated_files
#
# Simple function/macro which, given the base name of an idl file will generate
# the names of generated files.
#
define idl_generated_files =
  $(foreach suffix,$(DDS_FILES_SUFFIXES),$(1)$(suffix))
endef

# Now use idl_generated_files in conjunction with `foreach' to generate the
# names of all files generated by rtiddsgen.bat for all .idl files.
#
DDS_FILES_GENERATED := $(foreach idl_file,$(DDS_FILES_BASES), \
                         $(call idl_generated_files,$(idl_file)) \
                       )

# This pattern rule tells make how to build any of the generated files derived
# from a .idl file.  We use idl_generated_files with a parameter of `%' to
# create the target patterns for us.
#
$(call idl_generated_files,%): %.idl
    rtiddsgen.bat $<

# Rule to clean
clean:
    -rm -f $(DDS_FILES_GENERATED)

# Simple target for printing the value of a variable.  Useful for testing.
#
print-%:
    @printf '[$*] = [$($*)]\n'

通过一些简单的测试,我在目录中创建了两个文件test_a.idltest_b.idl 快速检查生成的文件名,即可得到...

ps1>进行打印-DDS_FILES_GENERATED

[DDS_FILES_GENERATED] = [test_b.h test_b.cxx test_bPlugin.h test_bPlugin.cxx test_bSupport.h test_bSupport.cxx test_a.h test_a.cxx test_aPlugin.h test_aPlugin.cxx test_aSupport.h test_aSupport.cxx

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM