Makefile通用模板


假设工程目录结构如下

src

    |-------

include

    |-------

Makefile


Makefile文件如下


CC=gcc
CXX=g++
CFLAGS=-Iinclude
CXXFLAGS=-Iinclude
LDFLAGS=-lpthread -lrt -lm

SRCDIRS=src
OBJPATH=

CFILES=$(foreach dir, $(SRCDIRS), $(wildcard $(dir)/*.c))
CPPFILES=$(foreach dir, $(SRCDIRS), $(wildcard $(dir)/*.cpp))

COBJS=$(CFILES:%.c=%.o)
CXXOBJS=$(CPPFILES:%.cpp=%.o)
OBJS=$(COBJS) $(CXXOBJS)
DEPS=$(OBJS:%.o=%.d)

TARGET=test

all: $(TARGET)

%.d: %.c
     @$(CC) $(CFLAGS) -MM $< | sed -e 's/\(.*\)\.o\(.*\)/\1\.d \1\.o\2/g' >$@
%.d: %.cpp
    @$(CC) $(CFLAGS) -MM $< | sed -e 's/\(.*\)\.o\(.*\)/\1\.d \1\.o\2/g' >$@

$(TARGET): $(OBJS)
     $(CXX) $(LDFLAGS) $^ -o $@

-include $(DEPS)

clean:
    rm -rf src/*.d src/*.o $(TARGET)

.PHONY: all clean</span>


你可能感兴趣的:(Makefile通用模板)