美文网首页
自动推导Makefile示例

自动推导Makefile示例

作者: 车到山前必有路2021 | 来源:发表于2022-01-16 12:10 被阅读0次
    CC = arm-openwrt-linux-muslgnueabi-gcc
    C++ = arm-openwrt-linux-muslgnueabi-g++
    LINK = arm-openwrt-linux-muslgnueabi-g++
    AR = arm-openwrt-linux-muslgnueabi-ar
    STRIP = arm-openwrt-linux-muslgnueabi-strip
    
    # user modify here
    LIB_PATHS = ./ 
    #
    INC_PATHS = ./ 
    #
    SRC_PATHS = ./ 
    #
    TARGET = sample_test
    TARGET_LIB =  libtest
    
    TARGET_STATIC = $(TARGET)_static
    TARGET_DYNAMIC = $(TARGET)_dynamic
    CFILES =  $(foreach n, $(SRC_PATHS), $(wildcard $(n)/*.c)) 
    C++FILES = $(foreach n, $(SRC_PATHS), $(wildcard $(n)/*.cc $(n)/*.cpp)) 
    
    DYNAMIC_LIBS_WITH_PATH = $(foreach n, $(LIB_PATHS), $(wildcard $(n)/*.so))
    STATIC_LIBS_WITH_PATH = $(foreach n, $(LIB_PATHS), $(wildcard $(n)/*.a))
    DYNAMIC_LIBS = $(foreach n, $(DYNAMIC_LIBS_WITH_PATH), -l$(patsubst lib%,%,$(patsubst %.so,%,$(notdir $(n)))))
    STATIC_LIBS  = $(foreach n, $(STATIC_LIBS_WITH_PATH), -l$(patsubst lib%,%,$(patsubst %.a,%,$(notdir $(n)))))
    
    INCLUDES = $(foreach n, $(INC_PATHS), -I$(n))
    LDFLAGS = $(foreach n, $(LIB_PATHS), -L$(n))
    
    STATIC_LDFLAGS = $(LDFLAGS) -Wl,-Bstatic -Wl,--start-group $(STATIC_LIBS) -Wl,--end-group -Wl,-Bdynamic -ldl  -lrt  -lpthread 
    DYNAMIC_LDFLAGS = $(LDFLAGS) -Wl,-Bdynamic -Wl,--start-group $(DYNAMIC_LIBS) -Wl,--end-group -ldl  -lrt  -lpthread 
    
    CCFLAGS = -c -g -fPIC  $(INCLUDES)
    C++FLAGS = -c -g -fPIC $(INCLUDES)
    OBJS = $(CFILES:%=%.o) $(C++FILES:%=%.o)
    DEPS = $(OBJS:%=%.d)
    
    
    all:$(TARGET_STATIC) $(TARGET_DYNAMIC)  # $(TARGET_LIB).so  $(TARGET_LIB).a
    
    $(TARGET_LIB).so : $(OBJS)
        $(CXX) -shared -o $@ $^
    $(TARGET_LIB).a : $(OBJS) 
        $(AR) -rcs -o $@  $^ 
    
    $(TARGET_STATIC): $(OBJS)
        @echo "==> build static..."
        $(LINK) -o $@  $^  $(STATIC_LDFLAGS)
        $(STRIP) $(TARGET_STATIC)
    
    $(TARGET_DYNAMIC): $(OBJS)
        @echo "==> build dynamic..."
        $(LINK) -o $@  $^  $(DYNAMIC_LDFLAGS)
        $(STRIP) $(TARGET_DYNAMIC)
    
    $(filter %.c.o , $(OBJS)): %.o:%
        $(CC) $(CCFLAGS) -o $@  $< -MD -MP -MF $(@:%=%.d)
    
    $(filter %.cpp.o %.cc.o, $(OBJS)): %.o:%
        $(C++) $(C++FLAGS) -o $@  $< -MD -MP -MF $(@:%=%.d)
    
    clean:
        rm -rf $(TARGET_LIB)*
        rm -rf $(TARGET_STATIC)
        rm -rf $(TARGET_DYNAMIC)
        rm -rf $(OBJS)
        rm -rf $(DEPS)
        rm -rf *.o
        rm -rf *.d
    
    -include $(DEPS)
    

    相关文章

      网友评论

          本文标题:自动推导Makefile示例

          本文链接:https://www.haomeiwen.com/subject/haupcrtx.html