makefile模板(可执行文件,动态库,静态库)

可执行文件:

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

CC=gcc
CC_FLAG=-Wall

PRG=test
OBJ=test.o

$(PRG):$(OBJ)
	$(CC) $(INC) $(LIB) -o $@ $(OBJ)
		      	
.SUFFIXES: .c .o
.c.o:
	$(CC) $(CC_FLAG) $(INC) -c $*.c -o $*.o

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

动态库:

############################################################# 
# Makefile for shared library.
#############################################################
#set your own environment option
CC = gcc
CC_FLAG =

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

#make target lib and relevant obj 
PRG = libtest.so
OBJ = test.o

#all target
all:$(PRG)

$(PRG):$(OBJ)
	$(CC) -shared -o $@ $(OBJ) $(LIB)
.SUFFIXES: .c .o
.c.o:
	$(CC) $(CC_FLAG) $(INC) -c $*.c -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 = gcc
CC_FLAG =

#static library use 'ar' command 
AR = ar

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

#make target lib and relevant obj 
PRG = libtest.a
OBJ = test.o

#all target
all:$(PRG)

$(PRG):$(OBJ)
	${AR} rv ${PRG} $?
.SUFFIXES: .c .o
.c.o:
	$(CC) $(CC_FLAG) $(INC) -c $*.c -o $*.o
.PRONY:clean
clean:
	@echo "Removing linked and compiled files......"
	rm -f $(OBJ) $(PRG)


你可能感兴趣的:(C,语言,linux,开发软件)