如何根据简单的源列表编写一个通用的makefile。

问题描述 投票:1回答:1

我有一个文件列表,比如:

SOURCES = program1.txt \ 
          program2.txt \
          ... \ 
          program_n.txt

我想在指定的情况下,从这些文件中生成C、Python等语言的处理程序,这些文件将在以后被一个顶级的makefile编译进来。

我如何编写我的makefile,以便从SOURCES中的每一个文件中,通过调用一个脚本来生成它的处理程序。

主要的目标应该是这样的。

all: handler_py handler_c ... handler_whatever

handler_py: all_my_possible_python_handlers

handler_c: all_my_possible_c_handlers

handler_whatever: all_my_possible_whatever_handlers

所以最后我们得到的是

sources/(name_of_program).txt produces output/handler_type/my_handler_(name_of_program).handler_type 

我的想法是,每当我写一个新的程序时,我只需要更新SOURCES列表。

而不是例如复制粘贴这个。

handler_py : output/my_handler_program.py output/my_handler_program1.py ... 

output/my_handler_program.py : sources/program.txt
    ./generate_handler.py py sources/program.txt

output/my_handler_program1.py : sources/program1.txt
    ./generate_handler.py py sources/program1.txt
...

linux makefile gnu-make gnu
1个回答
0
投票

直接使用 模式规则:

SOURCES = program1.txt program2.txt

handler_py: $(patsubst %.txt,output/my_handler_%.py,$(SOURCES))

output/my_handler_%.py : sources/%.txt
        ./generate_handler.py py $<
© www.soinside.com 2019 - 2024. All rights reserved.