通用Makefile

     编译当前目录下的多个目标:

ifdef VERBOSE
    VERBOSE=
else
    VERBOSE=@
endif
CROSS_COMPILE = 
LD = $(CROSS_COMPILE)gcc
CC = $(CROSS_COMPILE)gcc
CXX = $(CROSS_COMPILE)g++
CPP     ?= $(CC) -E
AR      ?= $(CROSS_COMPILE)ar
NM      ?= $(CROSS_COMPILE)nm
STRIP       ?= $(CROSS_COMPILE)strip
OBJCOPY     ?= $(CROSS_COMPILE)objcopy
OBJDUMP     ?= $(CROSS_COMPILE)objdump
RM      ?= rm -f
MAKEDIR     ?= mkdir -p



SRCS     = $(wildcard *.c)
OBJS     = $(patsubst %.c, %.o, $(SRCS))
TARGETS  = $(SRCS:%.c=%)
INCLUDE  = -I ./include
LIBS     = -L ./lib
$(info $(OBJS))
$(info $(TARGETS))

all : $(TARGETS)

$(TARGETS): %: %.o
        $(VERBOSE)mkdir -p `dirname $@`
        $(VERBOSE)echo "     Building $@"
	$(VERBOSE)$(CC) $(LIBS) $(LDFLAGS) $(LIBRARY) -o $@ $< -pthread

$(OBJS) : %.o: %.c 
	$(VERBOSE)$(CC) -c $(INCLUDE) $(CFLAGS) -o $@ $< -pthread


clean :
	$(VERBOSE)rm -rf $(TARGETS) $(OBJS) *~

#.SUFFIXES:
.PHONY : all clean

 

多个c文件编译一个目标:

CROSS_COMPILE = arm-none-linux-gnueabi-			#教材编译器
LD = $(CROSS_COMPILE)gcc
CC = $(CROSS_COMPILE)gcc
TARGET = test_app								#目标文件名

SRCS = $(wildcard ./src/*.c)
OBJS := $(SRCS:.c=.o)

INCLUDES =  -I ./inc 							#头文件路径
LIBS = -L ./lib -lpthread 						#库的路径及相关库
CCFLAGS = -g -Wall -O0							#告警及优化等级

all: $(TARGET)

%.o : %.c
	$(CC) -o $@ $(INCLUDES) $(CCFLAGS) -c $<  
$(TARGET) : $(OBJS)
	$(CC) -o $@ $^ $(LIBS)

clean :
	@rm -rf $(TARGETS) $(OBJS) *~

#.SUFFIXES:
.PHONY : all clean

 

你可能感兴趣的:(linux系统移植与驱动开发,makefile,通用Makefile,多个目标,多c文件)