几个常用makefile模板(动态库、静态库、可执行程序)

 APPNAME=link
LIB_SRCS=linker.c
LIB_NAME=linker
LIB_PATH=.libs

CFLAGS+=-I${LIB_PATH}
LDFLAGS+=-L${LIB_PATH} -l${LIB_NAME}

all: clean static_lib shared_lib install
    gcc ${CFLAGS} main.c -o ${APPNAME}_static ${LDFLAGS} -static
    gcc ${CFLAGS} main.c -o ${APPNAME}_shared ${LDFLAGS} 
    rm -f *.o

static_lib: linker.c
    gcc -c ${LIB_SRCS}
    ar -rcs lib${LIB_NAME}.a *.o
    rm -f *.o

shared_lib: 
     gcc -fPIC -shared ${LIB_SRCS}  -o lib${LIB_NAME}.so

clean:
     rm -rf *.o

distclean:
    rm -rf .libs
    rm -rf ${APPNAME}_*

install:
    mkdir -p ${LIB_PATH}
    mv lib${LIB_NAME}.* ${LIB_PATH}
    cp linker.h ${LIB_PATH}

 #编译静态库
AR=ar
TARGET=xx.a
obj= a.o b.o ....

all:$(obj)
$(AR) ruc $(TARGET) $^

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

#调用静态库
TARGET=targetName
CC=gcc
MKLIB=/xx/yy/xx.a
obj= a.o b.o ...

all:$(obj) $(MKLIB)
$(CC) $^ -o $(TARGET)

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

动态库编译
TARGET=lib_xx.so
CC=gcc
obj= a.o b.o ...
all:$(obj)
$(CC) -share -fPIC -o $(TARGET) $^

%.o:%.c
$(CC) -fpic -c $< $@

动态库调用
TARGET=targetName
CC=gcc
MKLIB=-L/xx/yy -l_xx
obj= a.o b.o ...

all:$(obj) 
$(CC) $(MKLIB) $^ -o $(TARGET)

%.o:%.c
$(CC) $(MKLIB) -c $< $@

#############################################################   
    # Makefile for shared library.  
    # 编译动态链接库  
#############################################################  
    #set your own environment option  
    CC = g++  
    CC_FLAG = -D_NOMNG -D_FILELINE  

    #set your inc and lib  
    INC =   
    LIB = -lpthread -L./ -lsvrtool  

    #make target lib and relevant obj   
    PRG = libsvrtool.so  
    OBJ = Log.o  

    #all target  
    all:$(PRG)  

    $(PRG):$(OBJ)  
        $(CC) -shared -o $@ $(OBJ) $(LIB)  

    .SUFFIXES: .c .o .cpp  
    .cpp.o:  
        $(CC) $(CC_FLAG) $(INC) -c $*.cpp -o $*.o  

    .PRONY:clean  
    clean:  
        @echo "Removing linked and compiled files......;  
        rm -f $(OBJ) $(PRG)  

#############################################################  
    # Makefile for static library.  
    # 编译静态链接库  
#############################################################  
    #set your own environment option  
    CC = g++  
    CC_FLAG = -D_NOMNG -D_FILELINE  

    #static library use 'ar' command   
    AR = ar  

    #set your inc and lib  
    INC =   
    LIB = -lpthread -L./ -lsvrtool  

    #make target lib and relevant obj   
    PRG = libsvrtool.a  
    OBJ = Log.o  

    #all target  
    all:$(PRG)  
    $(PRG):$(OBJ)  
        ${AR} rv ${PRG} $?  

    .SUFFIXES: .c .o .cpp  
    .cpp.o:  
        $(CC) $(CC_FLAG) $(INC) -c $*.cpp -o $*.o  

    .PRONY:clean  
    clean:  
        @echo "Removing linked and compiled files......"  
        rm -f $(OBJ) $(PRG)  

###########################################  
    #Makefile for simple programs  
###########################################  
    INC=  
    LIB= -lpthread  

    CC=CC  
    CC_FLAG=-Wall  

    PRG=threadpooltest  
    OBJ=CThreadManage.o CThreadPool.o CThread.o CWorkerThread.o threadpooltest.o  

    $(PRG):$(OBJ)  
        $(CC) $(INC) $(LIB) -o $@ $(OBJ)  

    .SUFFIXES: .c .o .cpp  
    .cpp.o:  
        $(CC) $(CC_FLAG) $(INC) -c $*.cpp -o $*.o  

    .PRONY:clean  
    clean:  
        @echo "Removing linked and compiled files......"  
        rm -f $(OBJ) $(PRG)