C/C++项目通用Makefile

时间:2022-03-16 12:40:50

该Makefile是在http://www.cnblogs.com/ggjucheng/archive/2012/01/03/2311107.html中下载源码得到的,那篇博文介绍了C++ JsonCpp的使用,里面的Makefile修改一下就能使用,保留别人的劳动成果,在这里共享了。

Makefile:

TARGET = main
OBJ_PATH = objs
PREFIX_BIN =

CC = g++
INCLUDES = -I include
LIBS =
CFLAGS =-Wall -Werror -O2 -g
LINKFLAGS =

SRCDIR = src/json src

#C_SOURCES = $(wildcard *.c)
C_SRCDIR = $(SRCDIR)
C_SOURCES = $(foreach d,$(C_SRCDIR),$(wildcard $(d)/*.c) )
C_OBJS = $(patsubst %.c, $(OBJ_PATH)/%.o, $(C_SOURCES))

#CPP_SOURCES = $(wildcard *.cpp)
CPP_SRCDIR = $(SRCDIR)
CPP_SOURCES = $(foreach d,$(CPP_SRCDIR),$(wildcard $(d)/*.cpp) )
CPP_OBJS = $(patsubst %.cpp, $(OBJ_PATH)/%.o, $(CPP_SOURCES))

default:init compile

$(C_OBJS):$(OBJ_PATH)/%.o:%.c
$(CC) -c $(CFLAGS) $(INCLUDES) $< -o $@

$(CPP_OBJS):$(OBJ_PATH)/%.o:%.cpp
$(CC) -c $(CFLAGS) $(INCLUDES) $< -o $@

init:
$(foreach d,$(SRCDIR), mkdir -p $(OBJ_PATH)/$(d);)

test:
@echo "C_SOURCES: $(C_SOURCES)"
@echo "C_OBJS: $(C_OBJS)"
@echo "CPP_SOURCES: $(CPP_SOURCES)"
@echo "CPP_OBJS: $(CPP_OBJS)"

compile:$(C_OBJS) $(CPP_OBJS)
$(CC) $^ -o $(TARGET) $(LINKFLAGS) $(LIBS)

clean:
rm -rf $(OBJ_PATH)
rm -f $(TARGET)

install: $(TARGET)
cp $(TARGET) $(PREFIX_BIN)

uninstall:
rm -f $(PREFIX_BIN)/$(TARGET)

rebuild: clean init compile