【问题标题】:Using nmake with wildcarded targets使用带有通配符目标的 nmake
【发布时间】:2020-12-08 09:43:33
【问题描述】:

使用nmake 我有以下makefile,它目前可以完成我需要它做的事情。 mycmd(正在运行的程序)将获取一个.inp 文件并生成一个.out 文件。我可以根据需要制作任意数量的.inp 文件,并且不需要更改makefile。它会找到它们并制作所有相关的.out 文件。

#####################################################################################
# A SUFFIXES declaration is required in order to later use the rule with target .inp.out
#####################################################################################
.SUFFIXES: .inp

#####################################################################################
# Here, NMAKE will expand *.inp in the prereq list for all, into the list of *.inp
# files in the directory, and then it will start a new NMAKE instance, specifying the
# goals to build all those files.
#####################################################################################
all: *.inp
  $(MAKE) $(**:.inp=.out)

#####################################################################################
# $(*B) represents the current target's base name minus the path and the file extension
#####################################################################################
.inp.out:
  mycmd -i $(*B).inp -o $(*B).out

我的问题是,如何进一步增强这个 makefile,以便我可以为一组 .inp 文件运行它,而不是 *.inp 而是说 ABC*.inp

【问题讨论】:

    标签: makefile nmake


    【解决方案1】:

    对您的makefile 进行简单修改即可。添加新的$(pattern) 宏:

    .SUFFIXES: .inp
    
    pattern = *                             # new macro; defaults to *
    
    all: $(pattern).inp                     # use it!
      @$(MAKE) -nologo $(**:.inp=.out)
    
    .inp.out:                               # dummy stub for testing
      @echo mycmd -i $(*B).inp -o $(*B).out
      @type NUL > $(*B).out
    

    然后在你的命令行中,覆盖pattern。例如,nmake -nologo pattern=ABC*


    更新:ma​​kefile 中的命令行:

      $(MAKE) $(**:.inp=.out)
    

    如果字符串 $** 太长,将失败并显示 fatal error U1095: expanded command line ... too long。在我的系统上,这发生在大约 32800 个字符处。

    在开头添加感叹号!(参见here)似乎不起作用,可能是因为没有简单的$**。有两种解决方法:

      !call set a=$** & call nmake %%a:.inp=.out%%
    

    或:

      !for %a in ($**) do nmake -nologo %~na.out
    

    这些都比你原来的慢两倍,有一个无所事事的mycmd存根。 (这里的for 循环并不是真正的循环,因为$** 只是一个单项。)

    【讨论】:

    • 编辑:也许会提到en.wikipedia.org/wiki/…
    • 谢谢!和 Har har 关于第二条评论:-D
    • 感谢您的更新。我的 makefile 不需要处理接近 32K 字符的文件,所以这个限制对我来说不是问题(谁会让文件名那么长!?)
    【解决方案2】:

    另一种解决方案是保留原始 makefile,并使用 DOS 命令,例如:

    for %a in (ABC*.inp) do nmake -nologo %~na.out
    

    这里的语法%~na 删除了变量%a 的扩展名。

    这比只使用 makefile 稍微慢一点,但也不慢。例如,对于 600 个 inp 文件和一个 mycmd 存根,在我的系统上,此命令需要 20 秒,而生成文件需要 15 秒。

    【讨论】:

      猜你喜欢
      • 2010-09-15
      • 2011-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-21
      • 1970-01-01
      相关资源
      最近更新 更多