当前位置: 移动技术网 > IT编程>开发语言>C/C++ > C、C++的Makefile模板

C、C++的Makefile模板

2019年09月27日  | 移动技术网IT编程  | 我要评论

张艺兴情定孙红雷,神探高伦布结局,1366 768壁纸

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模板。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网