Could you tell me how to achieve selective compilation?

now there is a C project. Suppose there are only main.c and pattern.h files, in which some standardized interfaces are defined

wants to achieve a function: provide the interface defined in pattern.h to user development, and eventually there will be a lot of c files containing the header file.

for example, classmate A writes a patternA.c to implement this interface,

classmate B writes a patternB.c to implement this interface,

both c files have -sharpinclude "pattern.h" and will exist in the same file directory at the same time.

finally in main.c or makefile , how do I choose one of them to compile the link?

for example, modify the macro definition in main.c or run make patternA or make patternB

is best implemented through the latter, which avoids the need for users to modify the source code during compilation

May.26,2022

Makefile example

all: patternA patternB

patternA: main.c patternA.c pattern.h
    gcc -o $@ $^

patternB: main.c patternB.c pattern.h
    gcc -o $@ $^    

then

  1. make or make all compile patternA and patternB at the same time
  2. make patternA compile patternA
  3. make patternB compile patternB
Menu