最近在看caffe工程的源代码,一个精简版的caffe工程目录代码如下:
.
├── CMakeLists.txt
├── data
├── include
│ └── caffe
│ ├── blob.hpp
│ ├── caffe.hpp
│ ├── common.hpp
│ ├── filler.hpp
│ ├── layer.hpp
│ ├── layers
│ ├── net.hpp
│ ├── proto
│ │ └── caffe_pretty_print.pb.h
│ ├── solver.hpp
│ ├── syncedmem.hpp
│ ├── util
│ │ ├── im2col.hpp
│ │ ├── insert_splits.hpp
│ │ ├── io.hpp
│ │ ├── math_functions.hpp
│ │ └── rng.hpp
│ └── vision_layers.hpp
├── Makefile
├── Makefile.config
├── src
│ ├── caffe
│ │ ├── blob.cpp
│ │ ├── blob.hpp
│ │ ├── caffe.hpp
│ │ ├── common.cpp
│ │ ├── common.hpp
│ │ ├── filler.hpp
│ │ ├── layer_factory.cpp
│ │ ├── layer.hpp
│ │ ├── layers
│ │ │ ├── bnll_layer.cpp
│ │ │ ├── bnll_layer.cu
│ │ │ ├── concat_layer.cpp
│ │ │ ├── concat_layer.cu
│ │ │ ├── conv_layer.cpp
│ │ │ ├── conv_layer.cu
| | | ...
│ │ │ ├── tanh_layer.cpp
│ │ │ ├── tanh_layer.cu
│ │ │ ├── window_data_layer.cpp
│ │ │ └── window_data_layer.cu
│ │ ├── net.cpp
│ │ ├── net.hpp
│ │ ├── proto
│ │ │ ├── caffe_pretty_print.proto
│ │ │ └── caffe.proto
│ │ ├── solver.cpp
│ │ ├── solver.hpp
│ │ ├── syncedmem.cpp
│ │ ├── syncedmem.hpp
│ │ ├── util
│ │ │ ├── im2col.cpp
│ │ │ ├── im2col.cu
│ │ │ ├── im2col.hpp
│ │ │ ├── insert_splits.cpp
│ │ │ ├── insert_splits.hpp
│ │ │ ├── io.cpp
│ │ │ ├── io.hpp
│ │ │ ├── math_functions.cpp
│ │ │ ├── math_functions.cu
│ │ │ ├── math_functions.hpp
│ │ │ ├── mkl_alternate.hpp
│ │ │ ├── rng.hpp
│ │ │ ├── upgrade_proto.cpp
│ │ │ └── upgrade_proto.hpp
│ │ └── vision_layers.hpp
以上工程包含了一般C++工程的一般目录结构,源代码目录src,头文件目录include,数据目录data等。针对C++工程目录可以以下Makefile作为模板进行修改,编写自己工程的Makefile:
# 定义工程名
PROJECT := caffe
TEST_GPUID := 0
# 可选项,一些环境变量的配置,变量太多可以写到一个独立的Makefile.config中
# 如果简单的话可以直接放在Makefile中定义
include Makefile.config
##############################################################################
# After this line, things should happen automatically.
##############################################################################
# The target static library and shared library name
NAME := lib$(PROJECT).so
STATIC_NAME := lib$(PROJECT).a
##############################
# 定义所有的源文件
##############################
# CXX_SRCS are the source files excluding the test ones.
CXX_SRCS := $(shell find src/$(PROJECT) ! -name "test_*.cpp" -name "*.cpp")
# HXX_SRCS are the header files
HXX_SRCS := $(shell find include/$(PROJECT) ! -name "*.hpp")
# CU_SRCS are the cuda source files
CU_SRCS := $(shell find src/$(PROJECT) -name "*.cu")
##############################
# 定义所有由源文件生成的文件名,如*.o, protobuf编译生成的.h, .cc文件等
##############################
# The generated files for protocol buffers
PROTO_GEN_HEADER := ${PROTO_SRCS:.proto=.pb.h}
PROTO_GEN_CC := ${PROTO_SRCS:.proto=.pb.cc}
PROTO_GEN_PY := ${PROTO_SRCS:.proto=_pb2.py}
# The objects corresponding to the source files
# These objects will be linked into the final shared library, so we
# exclude the tool, example, and test objects.
CXX_OBJS := $(addprefix $(BUILD_DIR)/, ${CXX_SRCS:.cpp=.o})
CU_OBJS := $(addprefix $(BUILD_DIR)/, ${CU_SRCS:.cu=.cuo})
PROTO_OBJS := $(addprefix $(BUILD_DIR)/, ${PROTO_GEN_CC:.cc=.o})
OBJS := $(PROTO_OBJS) $(CXX_OBJS) $(CU_OBJS)
##############################
# Derive include and lib directories
# 定义头文件目录和动态链接库路径
##############################
CUDA_INCLUDE_DIR := $(CUDA_DIR)/include
CUDA_LIB_DIR := $(CUDA_DIR)/lib64 $(CUDA_DIR)/lib
MKL_INCLUDE_DIR := $(MKL_DIR)/include
MKL_LIB_DIR := $(MKL_DIR)/lib $(MKL_DIR)/lib/intel64
INCLUDE_DIRS += ./src ./include $(CUDA_INCLUDE_DIR)
LIBRARY_DIRS += $(CUDA_LIB_DIR)
LIBRARIES := cudart cublas curand \
pthread \
glog protobuf leveldb snappy \
boost_system \
hdf5_hl hdf5 \
opencv_core opencv_highgui opencv_imgproc
PYTHON_LIBRARIES := boost_python python2.7
WARNINGS := -Wall
ifdef DEBUG
COMMON_FLAGS := -DDEBUG -g -O0
else
COMMON_FLAGS := -DNDEBUG -O2
endif
COMMON_FLAGS += $(foreach includedir,$(INCLUDE_DIRS),-I$(includedir))
CXXFLAGS += -pthread -fPIC $(COMMON_FLAGS)
NVCCFLAGS := -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
LDFLAGS += $(foreach librarydir,$(LIBRARY_DIRS),-L$(librarydir)) \
$(foreach library,$(LIBRARIES),-l$(library))
PYTHON_LDFLAGS := $(LDFLAGS) $(foreach library,$(PYTHON_LIBRARIES),-l$(library))
##############################
# Define build targets
##############################
.PHONY: all init test clean linecount lint tools examples py mat distribute \
py$(PROJECT) mat$(PROJECT) proto runtest
all: init $(NAME) $(STATIC_NAME) tools examples
@echo $(CXX_OBJS)
init:
@ mkdir -p $(foreach obj,$(OBJS),$(dir $(obj)))
@ mkdir -p $(foreach obj,$(TOOL_OBJS),$(dir $(obj)))
@ mkdir -p $(foreach obj,$(EXAMPLE_OBJS),$(dir $(obj)))
@ mkdir -p $(foreach obj,$(TEST_OBJS),$(dir $(obj)))
@ mkdir -p $(foreach obj,$(GTEST_OBJ),$(dir $(obj)))
# 代码统计,要安装cloc工具
linecount: clean
cloc --read-lang-def=$(PROJECT).cloc src/$(PROJECT)/
lint: $(LINT_REPORT)
$(NAME): init $(PROTO_OBJS) $(OBJS)
$(CXX) -shared -o $(NAME) $(OBJS) $(CXXFLAGS) $(LDFLAGS) $(WARNINGS)
@echo
$(STATIC_NAME): init $(PROTO_OBJS) $(OBJS)
ar rcs $(STATIC_NAME) $(PROTO_OBJS) $(OBJS)
@echo
$(OBJS): $(PROTO_GEN_CC) $(HXX_SRCS)
$(BUILD_DIR)/src/$(PROJECT)/%.o: src/$(PROJECT)/%.cpp
$(CXX) $< $(CXXFLAGS) -c -o $@
@echo
$(BUILD_DIR)/src/$(PROJECT)/layers/%.o: src/$(PROJECT)/layers/%.cpp
$(CXX) $< $(CXXFLAGS) -c -o $@
@echo
$(BUILD_DIR)/src/$(PROJECT)/proto/%.o: src/$(PROJECT)/proto/%.cc
$(CXX) $< $(CXXFLAGS) -c -o $@
@echo
$(BUILD_DIR)/src/$(PROJECT)/test/%.o: src/test/%.cpp
$(CXX) $< $(CXXFLAGS) -c -o $@
@echo
$(BUILD_DIR)/src/$(PROJECT)/util/%.o: src/$(PROJECT)/util/%.cpp
$(CXX) $< $(CXXFLAGS) -c -o $@
@echo
$(BUILD_DIR)/src/gtest/%.o: src/gtest/%.cpp
$(CXX) $< $(CXXFLAGS) -c -o $@
@echo
$(BUILD_DIR)/src/$(PROJECT)/layers/%.cuo: src/$(PROJECT)/layers/%.cu
$(CUDA_DIR)/bin/nvcc $(NVCCFLAGS) $(CUDA_ARCH) -c $< -o $@
@echo
$(BUILD_DIR)/src/$(PROJECT)/util/%.cuo: src/$(PROJECT)/util/%.cu
$(CUDA_DIR)/bin/nvcc $(NVCCFLAGS) $(CUDA_ARCH) -c $< -o $@
@echo
$(BUILD_DIR)/tools/%.o: tools/%.cpp
$(CXX) $< $(CXXFLAGS) -c -o $@ $(LDFLAGS)
@echo
$(BUILD_DIR)/examples/%.o: examples/%.cpp
$(CXX) $< $(CXXFLAGS) -c -o $@ $(LDFLAGS)
@echo
$(PROTO_GEN_PY): $(PROTO_SRCS)
protoc --proto_path=src --python_out=python $(PROTO_SRCS)
@echo
proto: $(PROTO_GEN_CC)
$(PROTO_GEN_CC): $(PROTO_SRCS)
protoc --proto_path=src --cpp_out=src $(PROTO_SRCS)
mkdir -p include/$(PROJECT)/proto
cp $(PROTO_GEN_HEADER) include/$(PROJECT)/proto/
@echo
clean:
@- $(RM) $(NAME) $(STATIC_NAME)
@- $(RM) $(PROTO_GEN_HEADER) $(PROTO_GEN_CC) $(PROTO_GEN_PY)
@- $(RM) include/$(PROJECT)/proto/$(PROJECT).pb.h
@- $(RM) python/$(PROJECT)/proto/$(PROJECT)_pb2.py
@- $(RM) python/$(PROJECT)/*.so
@- $(RM) -rf $(BUILD_DIR)
@- $(RM) -rf $(DISTRIBUTE_DIR)
distribute: all
mkdir $(DISTRIBUTE_DIR)
# add include
cp -r include $(DISTRIBUTE_DIR)/
# add tool and example binaries
mkdir $(DISTRIBUTE_DIR)/bin
cp $(TOOL_BINS) $(DISTRIBUTE_DIR)/bin
cp $(EXAMPLE_BINS) $(DISTRIBUTE_DIR)/bin
# add libraries
mkdir $(DISTRIBUTE_DIR)/lib
cp $(NAME) $(DISTRIBUTE_DIR)/lib
cp $(STATIC_NAME) $(DISTRIBUTE_DIR)/lib
# add python - it's not the standard way, indeed...
cp -r python $(DISTRIBUTE_DIR)/python
总的来说按照以下几步定义一个Makefile文件:
1. 定义工程名
2. 定义源文件
3. 定义目标文件(*.o)
4. 定义头文件搜索目录和动态链接库搜索目录
5. 定义目标
6. 编译规则,利用隐式编译规则%.cpp:%.o
关于Makefile的学习,请见附件或者找《跟我一起写Makefile-陈皓》学习。