makefile 实例: 编译libxyz.so 和 abc可执行文件。
.PHONY : clean all alldep
all: alldep
OBJDIR = Object
### libxyx.so ###
TARGET_LIB_XYZ = libxyz.so
TARGET_LIB_XYZ_OUTPUT = $(OBJDIR)/$(TARGET_LIB_XYZ)
LDFLAGS += -ltelux_wlan -lnl-3 -lnl-genl-3
LDFLAGS += -L$(SDKTARGETSYSROOT)/lib -L. -rdynamic
CFLAGS += -I$(SDKTARGETSYSROOT)/usr/include/xx
CFLAGS += -I$(SDKTARGETSYSROOT)/usr/include/yy
CFLAGS += -I$(SDKTARGETSYSROOT)/usr/include/zz
LIB_XYZ_SRC = a.c
LIB_XYZ_SRC += b.c
LIB_XYZ_SRC += c.c
LIB_XYZ_SRC += d.c
LIB_XYZ_SRC += e.cpp
LIB_XYZ_SRC += f.cpp
LIB_XYZ_SRC += g.cpp
LIB_XYZ_SRC_BASE = $(basename $(notdir $(LIB_XYZ_SRC)))
LIB_XYZ_SRC_DIR = $(addprefix $(OBJDIR)/, $(LIB_XYZ_SRC_BASE))
LIB_XYZ_SRC_OBJ = $(addsuffix .o, $(LIB_XYZ_SRC_DIR))
define make-cmd-cc
$2 : $1
@mkdir -p $(OBJDIR)
$$(CC) -shared -fPIC $$(CFLAGS) -MMD -MT $$@ -MF $$@.d -c -o $$@ $$<
endef
# build source files
$(foreach afile,$(LIB_XYZ_SRC),\
$(eval $(call make-cmd-cc,$(afile),\
$(addsuffix .o,$(addprefix $(OBJDIR)/,$(basename $(notdir $(afile))))))))
$(TARGET_LIB_XYZ_OUTPUT) : $(LIB_XYZ_SRC_OBJ)
$(CC) -o $@ -shared -fPIC $(LIB_XYZ_SRC_OBJ) $(LDFLAGS) $(CFLAGS)
### abc###
TARGET_BIN_ABC = abc
TARGET_BIN_ABC_OUTPUT = $(OBJDIR)/$(TARGET_BIN_ABC)
TARGET_BIN_SRC = a.c
TARGET_BIN_SRC += b.c
TARGET_BIN_BASE = $(basename $(notdir $(TARGET_BIN_SRC)))
TARGET_BIN_DIR = $(addprefix $(OBJDIR)/, $(TARGET_BIN_BASE))
TARGET_BIN_OBJ = $(addsuffix .o, $(TARGET_BIN_DIR))
define make-cmd-cc-exe
$2 : $1
@mkdir -p $(OBJDIR)
$$(CC) $$(CFLAGS) -MMD -MT $$@ -MF $$@.d -c -o $$@ $$<
endef
# build source files
$(foreach afile,$(TARGET_BIN_SRC),\
$(eval $(call make-cmd-cc-exe,$(afile),\
$(addsuffix .o,$(addprefix $(OBJDIR)/,$(basename $(notdir $(afile))))))))
$(TARGET_BIN_ABC_OUTPUT) : $(TARGET_BIN_OBJ)
$(CC) -o $@ $(TARGET_BIN_OBJ) $(LDFLAGS) $(CFLAGS)
### all ###
alldep: $(TARGET_LIB_XYZ_OUTPUT) $(TARGET_BIN_ABC_OUTPUT)
clean:
$(RM) -rf $(OBJDIR)
posted on 2024-02-27 15:22 huangguanyuan 阅读(29) 评论(0) 编辑 收藏 举报