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

本文深入解析了Makefile在构建简单程序、动态库与静态库过程中的应用,包括编译选项、链接命令及清理操作,帮助开发者高效管理构建流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

可执行文件:

###########################################
#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)


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值