Make:如何生成除一个以外的所有 .c 文件的列表

Make: How to generate list of all .c files except one

我有一个包含两个真实目标的 make 项目,一个测试套件和项目的可执行文件。

为了构建这些,我需要类似这样的规则:

testsuite: *.c (except executable.c) *.h
    clang ... *.c (except executable.c) -o testsuite

executable: *.c (except testsuite.c) *.h
    clang ... *.c (except testsuite.c) -o executable

如果可能的话,正确的语法是什么?

警告:我没有测试

您可以使用 testsuite: $(filter-out executable.c,$(wildcard *.c)) 之类的东西。我认为需要 wildcard 函数,因为您不想对字符串“*.c”进行操作,而是对它的扩展进行操作。先决条件会自动扩展,但函数参数不会。来自 info make:

Wildcard expansion is performed by 'make' automatically in targets and in prerequisites. In recipes, the shell is responsible for wildcard expansion. In other contexts, wildcard expansion happens only if you request it explicitly with the 'wildcard' function.

像这样应该可以满足您的需求。

# Explicitly list the sources specific to each target.
EXECUTABLE_SOURCES := executable.c
TESTSUITE_SOURCES := testsuite.c

# Filter out all specific sources from the common wildcard-ed sources.
COMMON_SOURCES := $(filter-out $(TESTSUITE_SOURCES) $(EXECUTABLE_SOURCES),$(wildcard *.c))

testsuite: $(TESTSUITE_SOURCES) $(COMMON_SOURCES)
        clang ....

executable: $(EXECUTABLE_SOURCES) $(COMMON_SOURCES)
        clang ....