Sources from subdirectories in Makefile

后端 未结 7 1337
无人共我
无人共我 2020-11-29 19:06

I have a C++ library built using a Makefile. Until recently, all the sources were in a single directory, and the Makefile did something like this

SOURCES = $(w

7条回答
  •  忘掉有多难
    2020-11-29 19:46

    Recursive wildcards can be done purely in Make, without calling the shell or the find command. Doing the search using only Make means that this solution works on Windows as well, not just *nix.

    # Make does not offer a recursive wildcard function, so here's one:
    rwildcard=$(wildcard $1$2) $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2))
    
    # How to recursively find all files with the same name in a given folder
    ALL_INDEX_HTMLS := $(call rwildcard,foo/,index.html)
    
    # How to recursively find all files that match a pattern
    ALL_HTMLS := $(call rwildcard,foo/,*.html)
    

    The trailing slash in the folder name is required. This rwildcard function does not support multiple wildcards the way that Make's built-in wildcard function does, but adding that support would be straightforward with a couple more uses of foreach.

提交回复
热议问题