patsubst on makefile

后端 未结 2 1891
耶瑟儿~
耶瑟儿~ 2020-12-28 22:35

I have to create different *.o files from a same set of *.c using various CFLAGS. I wanted to use patsubst to generate different *.o files from same *.c. I am doing somethin

相关标签:
2条回答
  • 2020-12-28 23:21

    Use patsubst to make lists of the objects that you want to build, and then use separate rules for each type of build.

    Something like this:

    SRC_FILES = source1.c source2.c 
    
    OBJ_FILES_O0 = $(patsubst %.c,%_O0.o,$(SRC_FILES)) 
    OBJ_FILES_O2 = $(patsubst %.c,%_O2.o,$(SRC_FILES))
    
    CFLAGS_O0 := -O0 
    CFLAGS_O2 := -O2
    
    all: $(OBJ_FILES_O0) $(OBJ_FILES_O2)
    
    $(OBJ_FILES_O0): %_O0.o: %.c
        $(CC) $(CFLAGS_O0) -c $< -o $@
    
    $(OBJ_FILES_O2): %_O2.o: %.c
        $(CC) $(CFLAGS_O2) -c $< -o $@
    
    0 讨论(0)
  • 2020-12-28 23:23

    You can also use wild cards to specify all files in the directory.

    eg:

    #Generic Makefile.
    
    CC := g++
    
    LD := g++
    
    CFLAGS := -c
    
    LDFLAGS := -L<path to lib> -l<libname> \
    
               -L<path to lib> -l>libname> \
                ......................
    
    ifeq (${TARGETARCH}, debug)
    
      CFLAGS += -g -O0
    
    elif 
    
      CFLAGS += -O4 -DNDEBUG
    
    SRCFILES := $(wildcard *.cpp)
    
    OBJFILES := $(patsubst %.cpp, %.o, ${SRCFILES})
    
    
    all: main
    
    main: ${OBJFILES}
    
      @echo "[Linking]"$@
    
      ${LD} ${LDFLAGS} ${OBJFILES}
    
    %.o: %.cpp
    
      @echo "[Compiling]"$@
    
      ${CC} ${CFLAGS} $^ -o $@
    
    0 讨论(0)
提交回复
热议问题