makefile常用模板

简介

每次想写个makefile都忘了咋写,大概还是要记一下的。

windows下的可用模板

Windows下的Makefile

windows下的makefile教程

Basic Win32 GUI application which can be built using MinGW.

大概如果要用mingw32-make来搞的话,直接参考linux下的写法即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

# This Makefile will build the MinGW Win32 application.

# Object files to create for the executable
OBJS = obj/AboutDialog.o obj/MainWindow.o obj/Resource.o obj/WinMain.o

# Warnings to be raised by the C compiler
WARNS = -Wall

# Names of tools to use when building
CC = gcc
RC = windres
TARGET = Win32App.exe

# Compiler flags. Compile ANSI build only if CHARSET=ANSI.
ifeq ($(CHARSET), ANSI)
CFLAGS = -O2 -std=c99 -D _WIN32_IE=0x0500 -D WINVER=0x0500 $(WARNS) -Iinclude
else
CFLAGS = -O2 -std=c99 -D UNICODE -D _UNICODE -D _WIN32_IE=0x0500 -D WINVER=0x0500 $(WARNS) -Iinclude
endif

# Linker flags
LDFLAGS = -s -lcomctl32 -Wl,--subsystem,windows

.PHONY: all clean


# Build executable by default
all: bin/$(TARGET)

# Delete all build output
clean:
[ -e bin/$(TARGET) ] && rm -rf bin/$(TARGET)
[ -d obj ] && rm -rf obj
#if exist bin\$(TARGET) ( del /q bin\$(TARGET) )
#if exist obj\* ( del /q obj\* )

# Create build output directories if they don't exist
bin obj:
[ ! -d $@ ] && mkdir $@
#if not exist $@ ( mkdir $@ )

# Compile object files for executable
obj/%.o: src/%.c | obj
$(CC) $(CFLAGS) -c $< -o $@

# Build the resources
obj/Resource.o: res/Resource.rc res/Application.manifest res/Application.ico include/Resource.h | obj
$(RC) -I./include -I./res -i $< -o $@

# Build the executable
bin/$(TARGET): $(OBJS) | bin
$(CC) -o $@ $(OBJS) $(LDFLAGS)

# C header dependencies
obj/AboutDialog.o: include/AboutDialog.h include/Globals.h include/Resource.h
obj/MainWindow.o: include/AboutDialog.h include/Globals.h include/MainWindow.h include/Resource.h
obj/WinMain.o: include/Globals.h include/MainWindow.h include/Resource.h

Linux

Linux环境C语言编程项目多级Makefile管理模板

大概是可用的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 排除目录
exclude_dirs := .git

SRCS = $(wildcard *.c ../lib/*.c) $(wildcard *.cpp ../lib/*.cpp) #wildcard把 指定目录 ./ 和 ../lib 下的所有后缀是c与cpp的文件全部展开。
OBJS = $(SRCS:.c = .o) #OBJS将$(SRCS)下的.c文件转化为.o文件
OUTPUT = test #输出程序名称,自己填

#编译参数
CC = gcc #代表所使用的编译器
CXX = g++
INCLUDES = -I../include \ #头文件查找路径
# -L指定库文件目录,-l指定静态库名字(去掉文件名中的lib前缀和.a后缀)
LIBS = -L../lib \ #链接库查找地址
CCFLAGS = -g -Wall -O1 #附加参数
CXXFLAGS:= $(CFLAGS) -DHAVE_CONFIG_H

#下面的基本上不需要做任何改动
all:$(OUTPUT)

$(OUTPUT) : $(OBJS)
mkdir -p output
$(CC) $^ -o $@ $(INCLUDES) $(LIBS)

%.o : %.c
$(CC) -c $< $(CCFLAGS)

rebuild: clean all

clean:
rm -rf *.out *.o #清除中间文件及生成文件

.PHONY:clean