C、C++的Makefile模板
程序员文章站
2022-06-01 11:05:18
[toc] Makefile模板 text CC = gcc LD = $(CC) TARGET = $(notdir $(CURDIR)) SRC_DIR = . INCLUDE_DIR += . C_FLAGS = g Wall LD_FLAFS = LD_LIBS = INCLUDES = I ......
makefile模板
cc = gcc ld = $(cc) target = $(notdir $(curdir)) src_dir = . include_dir += . c_flags = -g -wall ld_flafs = ld_libs = includes = -i$(include_dir) ifeq ($(cc), g++) type = cpp srcs += $(wildcard $(src_dir)/*.$(type)) objs += $(patsubst %.$(type), %.o, $(srcs)) else type = c srcs += $(wildcard $(src_dir)/*.$(type)) objs += $(patsubst %.$(type), %.o, $(srcs)) endif all : $(target) @echo "builded target:" $^ @echo "done" $(target) : $(objs) @echo "linking" $@ "from" $^ "..." $(ld) -o $@ $^ $(ld_flags) $(ld_libs) @echo "link finished\n" $(objs) : %.o:%.$(type) @echo "compiling" $@ "from" $< "..." $(cc) -c -o $@ $< $(c_flags) $(includes) @echo "compiled finished\n" .phony : clean cleanobj clean : cleanobj @echo "remove all executable file" rm -f $(target) cleanobj : @echo "remove binary files" rm -f *.o
用法
编译c程序
make
编译c++程序
make cc=g++
其他
target
指定生成的可执行文件名,我这里用的是当前所在目录名src_dir
指定源文件(.c .cpp)文件的路径include_dir
指定头文件路径c_flags
指定编译参数选项ld_flafs
指定链接参数选项ld_libs
指定链接库
清除生成的文件:
# 清空全部生成文件 make clean # 清空生成的中间文件 make cleanobj
tips
对于ubuntu系统,可以将makefile文件复制到~/templates(中文环境为~/模板)目录下,这样就可以在任意目录下右键添加该makefile模板。