寫了一個編譯靜態庫和動態庫的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 {GetProperty(Content)}lt;
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)