感谢:Makefile常用万能模板(包括静态链接库、动态链接库、可执行文件) https://www.bbsmax.com/A/Vx5MyvWv5N/
单文件下makefile编译成静态
######################################
#
#
######################################
#target you can change test to what you want
#共享库文件名,lib*.a
TARGET := libtest.a
#compile and lib parameter
#编译参数
CC := gcc
AR = ar
RANLIB = ranlib
LIBS :=
LDFLAGS :=
DEFINES :=
INCLUDE := -I.
CFLAGS := -g -Wall -O3 $(DEFINES) $(INCLUDE)
CXXFLAGS:= $(CFLAGS) -DHAVE_CONFIG_H
#i think you should do anything here
#下面的基本上不需要做任何改动了
#source file
#源文件,自动找所有.c和.cpp文件,并将目标定义为同名.o文件
SOURCE := $(wildcard *.c) $(wildcard *.cpp)
OBJS := $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(SOURCE)))
.PHONY : everything objs clean veryclean rebuild
everything : $(TARGET)
all : $(TARGET)
objs : $(OBJS)
rebuild: veryclean everything
clean :
rm -fr *.o
veryclean : clean
rm -fr $(TARGET)
$(TARGET) : $(OBJS)
$(AR) cru $(TARGET) $(OBJS)
$(RANLIB) $(TARGET)
优化修改递归所有文件夹生成静态文件
######################################
#
#
######################################
#target you can change test to what you want
#共享库文件名,lib*.a
TARGET := libaliy.a
#compile and lib parameter
#编译参数
CROSS_COMPILE := arm-linux-gnueabihf-
CC := $(CROSS_COMPILE)gcc
AR = $(CROSS_COMPILE)ar
RANLIB = $(CROSS_COMPILE)ranlib
LIBS :=
LDFLAGS := -lpthread
DEFINES :=
SRC_DIR := $(shell find . -type d )
INCLUDE := $(addprefix -I,$(SRC_DIR))
CFLAGS := -g -Wall -O3 $(DEFINES) $(INCLUDE)
CXXFLAGS:= $(CFLAGS) -DHAVE_CONFIG_H
#i think you should do anything here
#下面的基本上不需要做任何改动了
#source file
#源文件,自动找所有.c和.cpp文件,并将目标定义为同名.o文件
SOURCE := $(shell find $(SRC_DIR) -name "*.c")
OBJS := $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(SOURCE)))
.PHONY : everything objs clean veryclean rebuild
everything : $(TARGET)
all : $(TARGET)
objs : $(OBJS)
rebuild: veryclean everything
clean :
rm -fr $(shell find . -name *.o)
rm -rf *.a
veryclean : clean
rm -fr $(TARGET)
$(TARGET) : $(OBJS)
$(AR) cr $(TARGET) $(OBJS)
$(RANLIB) $(TARGET)