记录一个简单的makefile示例

编译共享库

######################################
# makefile shared library 
######################################
# set your all envrionment.
CC=g++
CC_FLAGS=-D_FILELINE
SRCS=$(wildcard *.cpp */*.cpp)

# set your all inc and lib
INCS=-I.
LIBS=-lpthread -L.  

#make taget lib and relevent obj
TARGET=libxxdk.so
OBJS=$(patsubst %.cpp, %.o, $(SRCS))

#all target 
all:$(TARGET)
$(TARGET):$(OBJS)
	$(CC) -shared -o $@ $^ $(LIBS)

%.o:%.cpp
	$(CC) $(CC_FLAGS) $(INCS) -c -fpic $< -o $@ -g

clean:
	rm -rf $(TARGET) $(OBJS)

编译静态库

######################################
# makefile static library 
######################################
# set your all envrionment.
CC=g++
AR=ar
CC_FLAGS=-D_FILELINE
SRCS=$(wildcard *.cpp */*.cpp)

# set your all inc and lib
INCS=-I.
LIBS=-lpthread -L.  

#make taget lib and relevent obj
TARGET=libxxdk.a
OBJS=$(patsubst %.cpp, %.o, $(SRCS))

#all target 
all:$(TARGET)
$(TARGET):$(OBJS)
	$(AR) crv $@ $^

%.o:%.cpp
	$(CC) $(CC_FLAGS) $(INCS) $(LIBS) -c $< -o $@  -g

.PHONY: clean
clean:
	rm -rf $(TARGET) $(OBJS)

编译可执行文件

######################################
# makefile executable 
######################################
# set your all envrionment.
CC=g++
CC_FLAGS=-D_FILELINE
SRCS=$(wildcard *.cpp */*.cpp)

# set your all inc and lib
INCS=-I.
LIBS=-lpthread -L.  

#make taget lib and relevent obj
TARGET=xxdk
OBJS=$(patsubst %.cpp, %.o, $(SRCS))

#all target 
all:$(TARGET)
$(TARGET):$(OBJS)
	$(CC) -o $@ $^

%.o:%.cpp
	$(CC) $(CC_FLAGS) $(INCS) $(LIBS) -c $< -o $@  -g

.PHONY: clean
clean:
	rm -rf $(TARGET) $(OBJS)
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值