CanTechLab

Can

Shell

135
2024-11-29

第7关 编译依赖静态库的目标

# target you can change what you want
TARGETSTATIC := lib/libstatic.a
TARGETSHAREHAL := lib/libsharedhal.so
TARGETEXECUTE := main

# compile and lib parameter
# 编译参数
CC := gcc
AR := ar crv
SHARED := -shared -fPIC
LIBS :=
LDFLAGS := -DEEP_DEV_BLOCK -DEEP_REPAIR_RECOVERY_IMAGE
LDFLAGS += -Wno-error=maybe-uninitialized -Wno-error=uninitialized -Wno-error=unused-parameter
CC += $(LDFLAGS)

# 头文件目录变量
INCLUDE := -I. -Iinc
CFLAGS := -g -Wall -O3 $(DEFINES) $(INCLUDE)
CXXFLAGS := $(CFLAGS) -DHAVE_CONFIG_H

# 目标文件路径
OBJDIR := lib

.PHONY: clean all

all: $(TARGETSTATIC) $(TARGETSHAREHAL) $(TARGETEXECUTE)

$(TARGETSTATIC): $(OBJDIR)/static_entry.o $(OBJDIR)/static_develop.o
	@mkdir -p $(OBJDIR)  # 确保目标目录存在
	$(AR) $@ $^  # 将目标文件打包成静态库

$(TARGETSHAREHAL): $(OBJDIR)/share_hal.o
	@mkdir -p $(OBJDIR)  # 确保目标目录存在
	$(CC) $(SHARED) -o $@ $^ $(LIBS)  # 编译成共享库

$(TARGETEXECUTE): $(OBJDIR)/main.o $(TARGETSTATIC) $(TARGETSHAREHAL)
	@mkdir -p $(OBJDIR)  # 确保目标目录存在
	$(CC) -o $@ $^ $(LIBS)  # 链接生成可执行文件

$(OBJDIR)/static_entry.o:
	$(CC) $(CFLAGS) -c src/static_entry.c -o $@

$(OBJDIR)/static_develop.o:
	$(CC) $(CFLAGS) -c src/static_develop.c -o $@

$(OBJDIR)/share_hal.o:
	$(CC) $(CFLAGS) -c src/share_hal.c -o $@ -fPIC

$(OBJDIR)/main.o:
	$(CC) $(CFLAGS) -c src/main.c -o $@

clean:
	rm -rf $(OBJDIR)/*.o $(OBJDIR)/*.i $(OBJDIR)/*.s
	rm -rf $(TARGETSTATIC) $(TARGETSHAREHAL) $(TARGETEXECUTE)

第8关 编译依赖动态库的目标

# target you can change what you want
TARGETSTATIC := lib/libstatic.a
TARGETSHAREHAL := lib/libsharedhal.so
TARGETEXECUTE := main

# compile and lib parameter
# 编译参数
CC := gcc
AR := ar crv
SHARED := -shared -fPIC
LIBS :=
LDFLAGS := -DEEP_DEV_BLOCK -DEEP_REPAIR_RECOVERY_IMAGE
LDFLAGS += -Wno-error=maybe-uninitialized -Wno-error=uninitialized -Wno-error=unused-parameter
CC += $(LDFLAGS)

# 头文件目录变量
INCLUDE := -I. -Iinc
CFLAGS := -g -Wall -O3 $(DEFINES) $(INCLUDE)
CXXFLAGS := $(CFLAGS) -DHAVE_CONFIG_H

# 目标文件路径
OBJDIR := lib

.PHONY: clean all

all: $(TARGETSTATIC) $(TARGETSHAREHAL) $(TARGETEXECUTE)

$(TARGETSTATIC): $(OBJDIR)/share_develop.o $(OBJDIR)/share_entry.o
	@mkdir -p $(OBJDIR)  # 确保目标目录存在
	$(AR) $@ $^  # 将目标文件打包成静态库

$(TARGETSHAREHAL): $(OBJDIR)/share_hal.o
	@mkdir -p $(OBJDIR)  # 确保目标目录存在
	$(CC) $(SHARED) -o $@ $^ $(LIBS)  # 编译成共享库

$(TARGETEXECUTE): $(OBJDIR)/main.o $(TARGETSTATIC) $(TARGETSHAREHAL)
	@mkdir -p $(OBJDIR)  # 确保目标目录存在
	$(CC) -o $@ $^ $(LIBS)  # 链接生成可执行文件

$(OBJDIR)/share_entry.o:
	$(CC) $(CFLAGS) -c src/share_entry.c -o $@ -fPIC

$(OBJDIR)/share_develop.o:
	$(CC) $(CFLAGS) -c src/share_develop.c -o $@ -fPIC

$(OBJDIR)/share_hal.o:
	$(CC) $(CFLAGS) -c src/share_hal.c -o $@ -fPIC

$(OBJDIR)/main.o:
	$(CC) $(CFLAGS) -c src/main.c -o $@

clean:
	rm -rf $(OBJDIR)/*.o $(OBJDIR)/*.i $(OBJDIR)/*.s
	rm -rf $(TARGETSTATIC) $(TARGETSHAREHAL) $(TARGETEXECUTE)

第9关 编译同时依赖静态库、动态库的目标

# target you can change what you want
TARGETSTATIC := lib/libstatic.a
TARGETSHAREHAL := lib/libsharedhal.so
TARGETEXECUTE := main

# compile and lib parameter
# 编译参数
CC := gcc
AR := ar crv
SHARED := -shared -fPIC
LIBS :=
LDFLAGS := -DEEP_DEV_BLOCK -DEEP_REPAIR_RECOVERY_IMAGE
LDFLAGS += -Wno-error=maybe-uninitialized -Wno-error=uninitialized -Wno-error=unused-parameter
CC += $(LDFLAGS)

# 头文件目录变量
INCLUDE := -I. -Iinc
CFLAGS := -g -Wall -O3 $(DEFINES) $(INCLUDE)
CXXFLAGS := $(CFLAGS) -DHAVE_CONFIG_H

# 目标文件路径
OBJDIR := lib

# 所有的源文件
SRCS := src/static_develop.c src/static_entry.c src/static_hal.c \
        src/share_develop.c src/share_entry.c src/share_hal.c \
        src/main.c

# 根据源文件生成目标文件的列表
OBJS := $(SRCS:src/%.c=$(OBJDIR)/%.o)

.PHONY: clean all

all: $(TARGETSTATIC) $(TARGETSHAREHAL) $(TARGETEXECUTE)


$(TARGETSTATIC) : $(OBJS)
############    注意使用TAB    ###############
    $(AR) $(TARGETSTATIC) $(OBJS)
#####################End#####################

$(TARGETSHAREHAL) : $(OBJS)
############    注意使用TAB    ###############
    $(CC) $(SHARED) $(OBJS) -o $(TARGETSHAREHAL) $(LIBS)
#####################End#####################

$(TARGETEXECUTE): $(OBJS)
############    注意使用TAB    ###############
    $(CC) $(OBJS) -o $(TARGETEXECUTE) $(LDFLAGS) $(LIBS)
#####################End#####################


# 编译每个源文件
$(OBJDIR)/%.o: src/%.c
    $(CC) $(CFLAGS) -c $< -o $@ -fPIC

clean:
    rm -fr $(OBJDIR)/*.o $(OBJDIR)/*.i $(OBJDIR)/*.s
    rm -fr $(TARGETSTATIC) $(TARGETSHAREHAL) $(TARGETEXECUTE)