欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

分享自己编写的一个比较通用的makefile

程序员文章站 2022-07-02 17:05:01
...
#############################################################################
# Author: jianwang16
# Date: 2017 06 18
# E-mail:  [email protected]
#############################################################################

CROSS_COMPILE ?=
TARGET   = main

#CUR_DIR  = $(shell pwd)
CUR_DIR  = .
SRCDIR   = $(CUR_DIR)/src
INCDIR 	 = $(CUR_DIR)/inc
OBJDIR   = $(CUR_DIR)/obj
LIB      = -lwiringPi
VPATH	 = src:inc

CC 		:= $(CROSS_COMPILE)gcc
CFLAGS   = -O2 -I$(INCDIR) -Wall -g -std=c99

# *.c->*.o
SOURCES  = $(foreach dir,$(SRCDIR),$(wildcard $(dir)/*.c))
#$(info SOURCES:$(SOURCES))
OBJECTS  = $(addprefix $(OBJDIR)/,$(patsubst %.c,%.o,$(notdir $(SOURCES))))
#$(info OBJECTS:$(OBJECTS))

all:$(TARGET)

$(TARGET):$(OBJECTS)
	$(info Building $(TARGET) with $(OBJECTS) and $(LIB)...)
	@$(CC) $(OBJECTS) -o $(TARGET) $(CFLAGS)  $(LIB)
	@chmod a+x $(TARGET)
	$(info Building [email protected] finished)

$(OBJDIR)/%.o:%.c $(INCLUDES) |  $(OBJDIR)
	$(info Compiling [email protected] from $<...)
	@$(CC) -c $(CFLAGS) $< -o [email protected]
	$(info Compiling [email protected] finished)
	$(info )
		
$(OBJDIR):
	@mkdir -p $(OBJDIR)

	
.PHONY:clean all	
clean:
	@$(RM) $(OBJDIR)/*.o main