我分享你我的例子生成文件(我不能在comment
部分鍵入所有的人,如果不回答你的問題,請讓我知道):
這裏是我的文件夾觀點:
./
├── debug
│ ├── debug.c
│ ├── debug.h
│ └── uart_print.c
├── driver
│ ├── driver.c
│ └── driver.h
├── include
│ └── common.h
├── Makefile
├── root
│ └── main.c
├── tc
│ ├── boot.c
│ ├── boot.h
│ ├── connect.c
│ ├── connect.h
│ ├── ffs.c
│ ├── ffs.h
│ ├── gpio.c
│ ├── gpio.h
│ ├── i2c.c
│ ├── i2c.h
│ ├── network.c
│ ├── network.h
│ ├── power.c
│ ├── power.h
│ ├── product.c
│ ├── product.h
│ ├── spi.c
│ └── spi.h
└── utility
├── utility.c
└── utility.h
Makefile1,沒有產生LIB
CC := gcc
LD := gcc
COLOR_ON := color
COLOR_OFF :=
PROGRAM = gSmokeTest
MAKE_DIR = $(PWD)
MODULES := debug driver utility tc root
SRC_DIR := $(addprefix $(MAKE_DIR)/,$(MODULES))
BUILD_DIR := $(MAKE_DIR)/output
SRC := $(foreach sdir,$(SRC_DIR),$(wildcard $(sdir)/*.c))
OBJ := $(patsubst %.c,%.o,$(SRC))
INCLUDES := $(addprefix -I,$(SRC_DIR))
INCLUDES += -I$(MAKE_DIR)/include
CFLAGS :=
CFLAGS += -Wall -O -ggdb -Wstrict-prototypes -Wno-pointer-sign #-Werror# -finstrument-functions -fdump-rtl-expand
#CFLAGS += -D_DEBUG_ -D_REENTRANT
TC_DEF :=
#TC_DEF += -D_POWER_SOS_
#TC_DEF += -D_PRODUCT_
#TC_DEF += -D_CONNECT_
TC_DEF += -D_SPI_
#TC_DEF += -D_I2C_
#TC_DEF += -D_POWER_
#TC_DEF += -D_BOOT_
#TC_DEF += -D_NETWORK_
vpath %.c $(SRC_DIR)
define make-goal
$1/%.o: %.c
@$(COLOR_ON)$(CC) $(TC_DEF) $(CFLAGS) $(INCLUDES) -c $$< -o [email protected]
@echo "Compile $$*.c"
endef
.PHONY: all checkdirs clean help flowchart
all: checkdirs $(BUILD_DIR)/$(PROGRAM)
$(BUILD_DIR)/$(PROGRAM): $(OBJ)
@$(LD) $^ -o [email protected]
@echo "Generate $(PROGRAM)"
checkdirs: $(BUILD_DIR)
$(BUILD_DIR):
mkdir -p [email protected]
clean:
rm -f $(OBJ) $(BUILD_DIR)/$(PROGRAM)
rm -rf $(BUILD_DIR)
help:
@echo "SRC DIR: $(SRC_DIR)"
@echo "Build DIR: $(BUILD_DIR)"
@echo "Source: $(SRC)"
@echo "Obj: $(OBJ)"
@echo "Includes: $(INCLUDES)"
flowchart:
@cflow2dot pdf ${SRC}
$(foreach sdir,$(SRC_DIR),$(eval $(call make-goal,$(sdir))))
然後,我設置了另一個文件夾結構(使之簡單,刪除一些文件),這時候我產生LIB:
./
├── debug
│ ├── debug.h
│ └──debug.mk
├── driver
│ ├── driver.h
│ ├── driver.mk
│ └── driver.o
├── include
│ └── common.h
├── libs
│ ├── libdebug.a
│ ├── libdrv.a
│ ├── libtc.a
│ └── libutility.a
├── make
│ ├── connect.d
│ ├── debug.d
│ ├── driver.d
│ ├── makefile
│ ├── network.d
│ ├── uart_print.d
│ └── utility.d
├── Makefile
├── prog
│ └── DEMO.map
├── root
│ ├── main.c
│ └── root.mk
├── rules.mk
├── tc
│ ├── connect.h
│ ├── network.h
│ └── tc.mk
├── tools.mk
└── utility
├── utility.h
└──utility.mk
Makefile2.1,頂層Makefile:
CC := gcc
LD := gcc
MAKE_DIR = $(PWD)
MODULES := root tc utility driver debug
SRC_DIR := $(addprefix ${MAKE_DIR}/,$(MODULES))
#BUILD_DIR := $(addprefix ${MAKE_DIR}/,$(MODULES))
SRC := $(foreach sdir,$(SRC_DIR),$(wildcard $(sdir)/*.c))
#OBJ := $(patsubst ${SRC_DIR}/%.c,${BUILD_DIR}/%.o,$(SRC))
#OBJ := $(foreach sdir,$(SRC_DIR),$(patsubst $(sdir)/%.c,$(BUILD_DIR)/%.o,$(filter $(sdir)/%,$(SRC))))
OBJ := $(patsubst %.c,%.o,$(SRC))
INCLUDES := $(addprefix -I,$(SRC_DIR))
vpath %.c $(SRC_DIR)
#SRC_F = $(foreach sdir,$(SRC_DIR),$(filter $(SRC_DIR)/%,$(SRC)))
#OBJ_F = $(patsubst $(SRC_F)/%.c, $(BUILD_DIR)/%.o, $(SRC_F))
default:
@echo "SRC DIR: ${SRC_DIR}"
# @echo "Build DIR: ${BUILD_DIR}"
@echo "Source: ${SRC}"
@echo "Obj: ${OBJ}"
@echo "Includes: ${INCLUDES}"
# @echo "SRC_F: ${SRC_F}"
# @echo "OBJ_F: ${OBJ_F}"
define make-goal
$1/%.o: %.c
$(CC) $(INCLUDES) -c $$< -o [email protected]
endef
all: test
test: $(OBJ)
$(LD) $^ -o [email protected]
#$(foreach sdir,$(SRC_DIR),$(eval $(call make-goal,$(sdir))))
#.PHONY: all checkdirs clean
#
#all: checkdirs build/test
#
#build/test: $(OBJ)
# $(LD) $^ -o [email protected]
#
#
#checkdirs: $(BUILD_DIR)
#
#$(BUILD_DIR):
# @mkdir -p [email protected]
#
clean:
rm -f $(OBJ) test
#
$(foreach sdir,$(SRC_DIR),$(eval $(call make-goal,$(sdir))))
#
,並在每個模塊中,我個人MK文件,生成自我庫:
#you just need change this MODULE name
MODULE = debug
LIB = $(LIB_DIR)/lib$(MODULE).a
SRCS = $(wildcard *.c)
OBJS = $(patsubst %.c, %.o, $(SRCS))
DEPS = $(patsubst %.c, %.d, $(SRCS))
$(LIB): $(OBJS)
@mkdir -p ../libs
@$(AR) cr [email protected] $^
@echo " Archive $(notdir [email protected])"
#$(OBJS): $(SRCS)
%.o: %.c
@$(CC) $(CFLAGS) -o [email protected] -c $<
@echo " CC [email protected]"
# Automatic dependency magic:
#%.d: %.c
# $(CC) -MM $(CFLAGS) [email protected] > $<
#-include (SRCS:%.c=%.d)
.PHONY: clean
clean:
@$(RM) -f $(LIB) $(OBJS) $(DEPS)
@$(RM) -f *.expand
@echo " Remove Objects: $(OBJS)"
@echo " Remove Libraries: $(notdir $(LIB))"
@echo " Remove Dependency: $(DEPS)"
.PHONY: lint
lint:
$(LINT) $(INC_SRCH_PATH) $(SRCS)
你使用'#include'或'#include「phase1.h」'?如果您使用第一種形式,則必須將'-I.'添加到'CFLAGS'中。 –
2015-02-10 05:35:04
我們正在使用第二種形式。 – Weasler 2015-02-10 05:36:07
我在閱讀那篇文章嗎?您的*庫存檔*正在添加'phase1.c'不習慣看到添加到lib檔案中的.c文件。 .o文件和其他.a文件,當然。 .c文件讓我去吧? – WhozCraig 2015-02-10 05:36:23