makefile编写注意事项

1、makefile的使用

1.1、一般makefile名字GNUmakefile, makefile and Makefile,在当前路径下执行make命令就会自动找到该GNUmakefile, makefile and Makefile文件执行编译。

make命令依次查找的文件是:GNUmakefile, makefile and Makefile.

GNUmakefile, is not recommended for most makefiles. You should use this name if you have a makefile that is specific to GNU make, and will not be understood by other versions of make. Other make
programs look for makefile and Makefile, but not GNUmakefile.

make

1.2、如果文件名不是GNUmakefile, makefile and Makefile,比如用makefile_name代替任意名字,执行编译时需要使用指定文件名编译,命令如下:

make -f makefile_name

1.3、清理编译缓存

make clean

2、makefile的一般结构

官方文档说makefile由5部分组成:显示规则、隐式规则、变量定义、指令和注释。

Makefiles contain five kinds of things: explicit rules, implicit rules, variable definitions,directives, and  comments.

2.1、编译目标,使用一个名称指代,比如:TARGET、EXECUTABLE等

比如官方提供的一个简单的makefile就是用edit指代编译目标,文件是逐个列出的,官方手册后面还写了几个基于这个的简化版本,即使这样,这种写法在实际应用中还是太繁琐了:

edit : main.o kbd.o command.o display.o \
insert.o search.o files.o utils.o
cc -o edit main.o kbd.o command.o display.o \
insert.o search.o files.o utils.o
main.o : main.c defs.h
cc -c main.c
kbd.o : kbd.c defs.h command.h
cc -c kbd.c
command.o : command.c defs.h command.h
cc -c command.c
display.o : display.c defs.h buffer.h
cc -c display.c
insert.o : insert.c defs.h buffer.h
cc -c insert.c
search.o : search.c defs.h buffer.h
cc -c search.c
files.o : files.c defs.h buffer.h command.h
cc -c files.c
utils.o : utils.c defs.h
cc -c utils.c
clean :
rm edit main.o kbd.o command.o display.o \
insert.o search.o files.o utils.o

2.2、自己理解的编译需要准备哪些东西

编译目标(目标名)、包含路径、链接库、编译优化选项、清除设置,具体可参考makefile万能模板。

下面会根据万能模板逐步介绍各个符号的含义。

3、例子

下面是一个链接cuda静态库的例子

TARGET :=./lib/libDLEngineOcr.so
LIBS := /home/xxx/build/lib/libcaffe.a /lib64/libcublas_static.a /lib64/libcublasLt_static.a /home/xxx/3dparty/lib/libprotoc.a /home/xxx/3dparty/lib/libprotobuf-lite.a /home/xxx/3dparty/lib/libprotobuf.a /home/xxx/3dparty/lib/libopenblas.a /home/xxx/3dparty/lib/libboost_system.a /home/xxx/3dparty/lib/libboost_thread.a /usr/local/cuda/lib64/libcudart_static.a /usr/local/cuda/lib64/libcurand_static.a /usr/local/cuda/lib64/libculibos.a

CFLAGS :=-g -Wall -O -fPIC -lm -lrt -m64 -I /home/xxx/caffeocrLinux/include/ -I /home/xxx/3dparty/include/ -I /usr/include/ -I /usr/local/cuda/include

CXXFLAGS := $(CFLAGS)
#ifneq ($(wildcard $(DJDIR)/bin/rm.exe),)
#RM-F := rm -f
#else
#RM-F := del
#endif
SOURCE := $(wildcard /*.c) $(wildcard *.cpp) 
OBJS := $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(SOURCE)))
DEPS := $(patsubst %.o,%.d,$(OBJS))
MISSING_DEPS := $(filter-out $(wildcard $(DEPS)),$(DEPS))
MISSING_DEPS_SOURCES := $(wildcard $(patsubst %.d,%.c,$(MISSING_DEPS)) \
$(patsubst %.d,%.cpp,$(MISSING_DEPS)))
CPPFLAGS +=-MD
.PHONY : everything deps objs clean veryclean rebuild dllLib
everything : $(TARGET)
deps : $(DEPS)
objs : $(OBJS)
clean :
	@$(RM) *.o
	@$(RM) *.d
veryclean: clean
	@$(RM) $(EXECUTABLE)
rebuild: veryclean everything
ifneq ($(MISSING_DEPS),)
$(MISSING_DEPS) :
	@$(RM) $(patsubst %.d,%.o,$@)
endif
-include $(DEPS)
$(EXECUTABLE) : $(OBJS) $(LIBS)
	g++ -shared -fPIC -m64 -pthread -lpthread -lrt -lm -o $(EXECUTABLE) $(OBJS) $(LIBS) 
$(LIBS):dllLib
dllLib:;

猜你喜欢

转载自blog.csdn.net/juluwangriyue/article/details/114543077