Flat object file directory structure output with GNU Make

前端 未结 5 2336
遥遥无期
遥遥无期 2020-12-13 17:11

I have a C++ small project using GNU Make. I\'d like to be able to turn the following source files:

src/
  a.cpp
  b/
    b.cpp
  c/
    c.cpp
5条回答
  •  轮回少年
    2020-12-13 17:21

    Creating the flat directory structure as initially requested.

    Does use vpath to track down the source files, but all the bookeeping is done automagically from the SRC list.

    SRC = a/a.c a/aa.c b/b.c
    TARGET = done
    
    FILES = $(notdir $(SRC) )
    #make list of source paths, sort also removes duplicates
    PATHS = $(sort $(dir $(SRC) ) )
    
    BUILD_DIR = build
    OBJ = $(addprefix $(BUILD_DIR)/, $(FILES:.c=.o))
    DEP = $(OBJ:.o=.d)
    
    # default target before includes
    all: $(TARGET)
    
    include $(DEP)
    
    vpath %.c $(PATHS)
    
    # create dummy dependency files to bootstrap the process
    %.d:
        echo a=1 >$@
    
    $(BUILD_DIR)/%.o:%.c
        echo $@: $< > $(patsubst %.o,%.d,$@)
        echo $< >$@
    
    $(TARGET): $(OBJ)
        echo $^ > $@
    
    .PHONY: clean
    clean:
        del $(BUILD_DIR)/*.o
        del $(BUILD_DIR)/*.d
    

    Sorry about the ugly echo's but I only had my win box to test it on.

提交回复
热议问题