faraway  

写了一个编译静态库和动态库的Makefile文件,修改Makefile中STATIC的值可以编译静态库或动态库。完整的程序和Makefile文件在这里

MakefileSTATIC=0

LIBPATH=lib
CFLAGS=-Wall
LDFLAGS=-I$(LIBPATH) -L$(LIBPATH) -lfunc

ifeq ($(STATIC), 1)
LIB=$(LIBPATH)/libfunc.a
else
LIB=$(LIBPATH)/libfunc.so
CFLAGS+=-fPIC
endif

TARGET=main
SRC=main.c
LIBSRC=libinc.c libdec.c
LIBOBJ=$(addprefix $(LIBPATH)/, $(addsuffix .o, $(basename $(LIBSRC))))

all: $(LIB) $(TARGET)

target: $(TARGET)

lib: $(LIB)

$(LIBPATH)/%.o: %.c
    $(CC) $(CFLAGS) -o $@ -c $<

ifeq ($(STATIC), 1)
$(LIB): $(LIBOBJ)
    $(AR) -rcs $@ $^
else
$(LIB): $(LIBOBJ)
    $(CC) -shared -o $@ $^
endif

$(TARGET): $(SRC)
    $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)

clean:
    rm -f $(TARGET) $(LIB) $(LIBOBJ)

 

 

posted on 2009-03-31 18:50  faraway  阅读(4335)  评论(0编辑  收藏  举报