有时候经常需要写一些小的测试用例,有一个Makefile会比较方便。于是就想到写一个比较通用的Makefile。
github地址:https://github.com/RichardUSTC/general-makefile
这个Makefile的特点:
- 支持C和C++程序
- 每个C/C++源文件编译成一个可执行程序
- 根据需要,可以通过make dump来dump出可执行程序的汇编代码,编译分析
- make时可以在命令行指定CC、CXX、CFLAGS、CXXFLAGS、LDFLAGS、LIBS、OBJDUMP和DUMP_FLAGS变量。例如make LIBS=-lpthread。注意这时等号两侧不能有空格
- 编译出来的程序带有调试信息
- 使用时,放在源码所在目录即可
CC = gcc CXX = g++ OBJDUMP = objdump INCLUDES += CFLAGS += -g CXXFLAGS += -g LDFLAGS += LIBS += DUMP_FLAGS = -d ifneq (${INCLUDES}, ) CFLAGS += -I${INCLUDES} CXXFLAGS += -I${INCLUDES} endif C_SRC = $(wildcard *.c) C_TARGET = $(patsubst %.c, %, ${C_SRC}) C_DUMP= $(patsubst %.c, %.dump, ${C_SRC}) CPP_SRC = $(wildcard *.cpp) CPP_TARGET = $(patsubst %.cpp, %, ${CPP_SRC}) CPP_DUMP= $(patsubst %.cpp, %.dump, ${CPP_SRC}) TARGET = ${C_TARGET} ${CPP_TARGET} DUMP = ${C_DUMP} ${CPP_DUMP} all: ${TARGET} dump: ${DUMP} ${TARGET} clean: @rm -f ${TARGET} ${DUMP} ${C_TARGET}: %: %.c ${CC} -o $@ $< ${CFLAGS} ${LDFLAGS} ${LIBS} ${CPP_TARGET}: %: %.cpp ${CXX} -o $@ $< ${CXXFLAGS} ${LDFLAGS} ${LIBS} ${DUMP}: %.dump: % ${OBJDUMP} ${DUMP_FLAGS} $< > $@