搜索

Google
 

星期四, 三月 08, 2007

Makefile奇技淫巧

自从用了autotools很久不曾有过编写Makefile的经历了,这两天维护客户以前让别的公司给做的一个项目中的代码,写了个Makefile感觉挺不错。。。

CC = g++

GTK_CFLAGS = $(shell gdk-pixbuf-config --cflags)
GTK_LIBS = $(shell gdk-pixbuf-config --libs) $(shell pkg-config --libs gthread)

MYSQL_CFLAGS = $(shell mysql_config --cflags)
MYSQL_LIBS = $(shell mysql_config --libmysqld-libs)

CFLAGS = $(GTK_CFLAGS) $(MYSQL_CFLAGS) -I/usr/include/lclibs -DMYSQL_EMBED -DETC_THREAD
LIBS = $(GTK_LIBS) $(MYSQL_LIBS) -L./libs -ldl -rdynamic -llclibs -ldsrc -lgts

SRC_PATH = src
OBJ_PATH = obj

SOURCES = $(wildcard $(SRC_PATH)/*.c)
OBJECTS = $(patsubst $(SRC_PATH)/%.c, $(OBJ_PATH)/%.o, $(SOURCES))
DEPENDS = $(patsubst $(SRC_PATH)/%.c, $(OBJ_PATH)/%.d, $(SOURCES))

all: hwetc

$(DEPENDS): $(OBJ_PATH)/%.d: $(SRC_PATH)/%.c
$(CC) -MM $(CFLAGS) $< > $@; \
sed -i 's,\($*\)\.o[ :]*,$(OBJ_PATH)/\1.o $@ : ,g' $@

include $(DEPENDS)

hwetc: $(OBJECTS)
$(CC) $(OBJECTS) $(LIBS) -o $@

$(OBJECTS):
$(CC) -c $(CFLAGS) $< -o $@

.PHONY: clean
clean:
rm -f hwetc $(OBJECTS) $(DEPENDS)

没有评论: