个人用惯了cmake,从没有写过makefile, 突然领导交代要给一个小工程写个makefile。
硬着头皮花了1天的时间,写出来一个,才发现原来makefile也不是那么的难已忍受,
新技能get,贴个模板以备以后使用:
DIR_obj := ./build
DIR_LIB := ./lib
SRC += $(wildcard *.cpp)
OBJ := $(patsubst %.cpp, $(DIR_obj)/%.o, $(SRC))
SHARE_LIB := $(DIR_LIB)/libneuralnetworks.so
STATIC_LIB := $(DIR_LIB)/libneuralnetworks.a
CFLAGS += -fPIC -std=c++11
LDFLAGS += -shared -fPIC
SIMULATOR_SDK_DIR = /home/kg/work/nnapi/simulator4/build/sdk
INCLUDE += -I$(SIMULATOR_SDK_DIR)/include
DIRVER_DIR += $(SIMULATOR_SDK_DIR)/drivers
LIBS += $(wildcard $(SIMULATOR_SDK_DIR)/drivers/*.so)
LINK_LIBS += $(patsubst %.so, -L %.so, $(LIBS))
ifeq ($(NN_DUMP), 1)
SRC += $(wildcard json/*.cpp)
OBJ += $(patsubst json/%.cpp, $(DIR_obj)/json/%.o, $(wildcard json/*.cpp))
CFLAGS += -DDUMP_NEURALNETWORK
endif
ifeq ($(NN_DEBUG), 1)
CFLAGS += -DNN_DEBUG
endif
ifeq ($(FORMAT_SWITCH), 1)
CFLAGS += -DFORMAT_SWITCH
endif
all:$(OBJ)
@echo src: $(SRC)
@echo comiling... $(OBJ)
@echo libs: $(LIBS)
@echo LINK_libs: $(LINK_LIBS)
@echo include: $(INCLUDE)
g++ $(LDFLAGS) -o $(SHARE_LIB) $(OBJ)
$(DIR_obj)/%.o:%.cpp
gcc -g $(CFLAGS) -c $< -o $@ $(INCLUDE)
ifeq ($(NN_DUMP), 1)
$(DIR_obj)/json/%.o:json/%.cpp
@if test ! -d $(DIR_obj)/json;\
then\
mkdir $(DIR_obj)/json;\
fi
gcc -g $(CFLAGS) -c $< -o $@ $(INCLUDE)
endif