Makefile 编译两个模块一次可以生成client 和 server的可执行文件

demo有 client和server 使用一个makefile 来编译

1.建立文件目录

|-- Makefile
|-- bin
| |-- receive
| -- send |-- include | |-- Log.h | |-- commonfun.h | |-- heartbeat.h |– udpnet.h
|-- obj
| |-- Log.o
| |-- commonfun.o
| |-- heartbeat.o
| |-- heartbeatMonitor.o
| |-- heartbeatMonitor_send.o
| -- udpnet.o |-- readme– src
|-- Log.cpp
|-- commonfun.cpp
|-- heartbeat.cpp
|-- recv
| -- heartbeatMonitor.cpp |-- send |– heartbeatMonitor_send.cpp
`-- udpnet.cpp

2. Makefile内容:


#define compiler var.
GCC = gcc -pthread
GXX = g++ -pthread

#define compiler switch.
CFLAG = -Wall -c # -Wall显示所有警告,-g 可调试,-c编译 

#define header of files dir.
INCLUDE = ./include

#define compiler source code dir.
SRC_DIR = ./src
SRC_SD = ./src/send
SRC_RE = ./src/recv

SRC = $(wildcard $(SRC_DIR)/*.cpp) # wildcard 获取目录下文件
SRC_RECV = $(wildcard $(SRC_RE)/*.cpp)
SRC_SEND = $(wildcard $(SRC_SD)/*.cpp)

#define output dir for object files.
OBJOUTPUT = ./obj/

#define output dir for bin
BINOUTPUT = ./bin/

OBJS_SEND = $(patsubst %.cpp,%.o,$(SRC) $(SRC_SEND)) #patsubt cpp 直接转化成 .o
OBJS_RECV = $(patsubst %.cpp,%.o,$(SRC) $(SRC_RECV))

OBJS_DIR_SEND = $(addprefix $(OBJOUTPUT),$(notdir $(OBJS_SEND))) #notdir 去除路径信息 直接转化成 .O
OBJS_DIR_RECV = $(addprefix $(OBJOUTPUT),$(notdir $(OBJS_RECV))) #addprefix  组合字符即添加前缀

%.o:%.cpp
	$(GXX) $(CFLAG) $< -I$(INCLUDE) -o $@ 
	mv $@ $(OBJOUTPUT) 

# $@ = %.o (*.o)

send:$(OBJS_SEND)$
	$(GXX) $(OBJS_DIR_SEND) -o $(BINOUTPUT)$@

receive:$(OBJS_RECV)$
	$(GXX) $(OBJS_DIR_RECV) -o $(BINOUTPUT)$@

clean:
	rm -rf $(OBJOUTPUT)*.o $(BINOUTPUT)*

ECHO:
	@echo $(OBJS_SEND) 
	@echo "=========="
	@echo $(OBJS_RECV)
	@echo "=========="
	@echo $(OBJS_DIR_SEND)
	@echo "=========="
	@echo $(OBJS_DIR_RECV)
	@echo "=========="	
	@echo $@

all: send receive ECHO
.PHONY : all

3.使用方法

编译所有模块
make all
单编译模块:
make send
make recv
make ECHO

你可能感兴趣的:(Makefile)