How to compile different c files with different CFLAGS using Makefile?

前端 未结 3 1075
无人及你
无人及你 2020-12-23 09:54

all. Let\'s say I have a program that contains a long list of C source files, A.c, B.c, ...., Z.c, now I want to compile A.c, B.c with certain CFLAGS, and compile the rest p

相关标签:
3条回答
  • 2020-12-23 10:28

    I can't answer the question for raw makefiles, but if you are willing to use automake it is trivial:

    foo_CFLAGS = [options passed to CC only when building foo]
    
    0 讨论(0)
  • 2020-12-23 10:33

    The approach taken by linux kernel build system:

    CFLAGS += $(CFLAGS-$@)
    

    And then,

    CFLAGS-A.o += -DEXTRA
    CFLAGS-B.o += -DEXTRA
    
    0 讨论(0)
  • 2020-12-23 10:42

    Try using target-specific variables. A target-specific variable is declared like this:

    TARGET: VAR := foo  # Any valid form of assignment may be used ( =, :=, +=, ?=)
    

    Now when the target named TARGET is being made, the variable named VAR will have the value "foo".

    Using target-specific variables, you could do this, for example:

    OBJ=[all other .o files here, e.g. D.o, D.o, E.o .... Z.o]
    SPECIAL_OBJS=A.o B.o
    
    all: $(OBJ) $(SPECIAL_OBJS)
    
    $(SPECIAL_OBJS): EXTRA_FLAGS := -std=c99   # Whatever extra flags you need
    
    %.o: %.c
         @echo [Compiling]: $<
         $(CC) $(CFLAGS) $(EXTRA_FLAGS) -o $@ -c $<
    
    0 讨论(0)
提交回复
热议问题