2010-10-07 23:11

[C/C++語言] Makefile 通用範例


SRC_DIR = src
OBJ_DIR = obj

SOURCES = \
$(SRC_DIR)/test2.cpp \

TARGET = main.exe


# =================================================
INCLUDE_PATH = \
#-I"include_path" \

NEXUSMGR_LIBDIR = \
#-L"library_path" \

CXXFLAGS = -O0 -g3 -Wall -fPIC -w -c -fmessage-length=0
CFLAGS = -O0 -g3 -Wall -fPIC -w -c -fmessage-length=0

LIBS = \
#-lsqlite \

CC := gcc
CXX := g++
RM := del /Q

# =================================================
OBJS:=$(subst $(SRC_DIR),$(OBJ_DIR),$(SOURCES))
OBJS:=$(OBJS:%.cpp=%.cpp.o)
OBJS:=$(OBJS:%.C=%.C.o)
OBJS:=$(OBJS:%.c=%.c.o)

$(OBJ_DIR)/%.cpp.o: $(SRC_DIR)/%.cpp
$(CXX) $(CXXFLAGS) $(INCLUDE_PATH) -MMD -MP -MF $(@:%.o=%.d) -MT $(@:%.o=%.d) -o $@ $<

$(OBJ_DIR)/%.C.o: $(SRC_DIR)/%.C
$(CXX) $(CXXFLAGS) $(INCLUDE_PATH) -MMD -MP -MF $(@:%.o=%.d) -MT $(@:%.o=%.d) -o $@ $<

$(OBJ_DIR)/%.c.o: $(SRC_DIR)/%.c
$(CC) $(CFLAGS) $(INCLUDE_PATH) -MMD -MP -MF $(@:%.o=%.d) -MT $(@:%.o=%.d) -o $@ $<


$(TARGET): $(OBJS)
$(CXX) $(NEXUSMGR_LIBDIR) -o $(TARGET) $(OBJS) $(LIBS)

$(OBJ_DIR):
-mkdir $(OBJ_DIR)

all: $(OBJ_DIR) $(TARGET)

clean:
-$(RM) $(OBJ_DIR) $(TARGET)

0 回應: