最近因工作需要,要尝试徒手撸一份makefile文件,这份模版支持批量化,也不针对某一个C/CPP文件指定规则:
# Makefile for building: GuiDemo
# by MT 2023-12-12 v1.0
# http://blog.csdn.net/wangningyu
CC = gcc
CFLAGS = -std=c11 -Wall
TARGET = ./bin/GuiDemoSRC_FLASH = flash
SRC_GUI = gui/Gui
SRC_MISC = gui/Misc
SRC_USER = gui/User
SRC_FONT = gui/User/Fonts/INC_ROOT = .
INC_DIR = gui/
INC_FLASH = flash/
INC_GUI = gui/Gui/
INC_MISC = gui/Misc/
INC_USER = gui/User/
INC_FONT = gui/User/Fonts/
INC_SDL2 = SDL2/32/include/SDL2/
INCLUDE_DIR = -I$(INC_ROOT) -I$(INC_DIR) -I$(INC_FLASH) -I$(INC_GUI) -I$(INC_MISC) -I$(INC_USER) -I$(INC_FONT) -I$(INC_SDL2)LIB_DIR = SDL2/32/lib/
LIBS = -lSDL2 -lSDL2main#获取所有的.c文件
SOURCES = $(wildcard *.c $(SRC_FLASH)/*.c $(SRC_GUI)/*.c $(SRC_MISC)/*.c $(SRC_USER)/*.c $(SRC_FONT)/*.c)#将.c文件替换成.o文件
OBJECTS = $(SOURCES:.c=.o)#链接规则
$(TARGET): $(OBJECTS)$(CC) $(CFLAGS) -o $@ $^ -L$(LIB_DIR) $(LIBS)#编译规则
%.o: %.c$(CC) $(CFLAGS) -c -o $@ $< $(INCLUDE_DIR)#.PHONY: cleanclean:@echo "Cleaning..."del /f /s /q .\*.o
支持MinGW,需要的直接拷贝走,免得以后再去写了~~