全部博文(776)
分类: LINUX
2010-02-22 20:33:35
写了一个编译静态库和动态库的Makefile文件,修改Makefile中STATIC的值可以编译静态库或动态库。
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)