【问题标题】:Create one Makefile rule for several files为多个文件创建一个 Makefile 规则
【发布时间】:2020-03-04 13:03:03
【问题描述】:

我想只创建一个 Makefile 规则,而不是为同一类型的不同文件创建相同的规则。

考虑这个小例子,files 声明了两个 Python-Notebook,每个 Notebook 都应转换为 Python-Script (.py)。这可以通过命令jupyter nbconvert --to script FILE 来完成。我尝试创建的 Makefile 看起来像这样,但 工作:

files = A.ipynb B.ipynb

all: convert

convert: $(files)   
     jupyter nbconvert --to script $<      # How to define $< as the changed file?

现在,每当 A.ipynb 更改时,我想要命令

jupyter nbconvert --to script A.ipynb

被执行。这可以在上面“建议”的一个简单规则内完成吗?我可以编写一个 Makefile 来完成这项任务,但它更长,并且随着 files 的大小缩放:

all: A.py B.py

A.py: A.ipynb
    jupyter nbconvert --to script $<

B.py: B.ipynb
    jupyter nbconvert --to script $<

【问题讨论】:

    标签: makefile gnu-make


    【解决方案1】:

    这就是pattern rules 的用途:

    all: A.py B.py
    
    %.py: %.ipynb
            jupyter nbconvert --to script $<
    

    您也可以使用某些人喜欢的static pattern rules

    【讨论】:

    • 谢谢!正在阅读有关静态模式的信息,但无法进行转移。那么这个$(files): %.py: %.ipynb jupyter nbconvert --to script $&lt; 会是静态模式吗?
    • 是的,假设files := A.py B.py
    猜你喜欢
    • 2011-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多