[tab] 是製表符
# 指定編譯器
CC = gcc
# CFLAG包括頭文件目錄
CFLAGS = -g -Wall
# 頭文件查找路徑
INC = -Iport -I../../modbus/rtu \
-I../../modbus/ascii -I../../modbus/include -I../../modbus/tcp
# 靜態鏈接庫
LDFLAGS =
LDLIBS = -lpthread
# 目標
TARGET = tcpmodbus
# 源文件
SRC = demo.c port/portother.c \
port/portevent.c port/porttcp.c \
../../modbus/mb.c ../../modbus/tcp/mbtcp.c \
../../modbus/functions/mbfunccoils.c \
../../modbus/functions/mbfuncdiag.c \
../../modbus/functions/mbfuncholding.c \
../../modbus/functions/mbfuncinput.c \
../../modbus/functions/mbfuncother.c \
../../modbus/functions/mbfuncdisc.c \
../../modbus/functions/mbutils.c
# 源文件編譯爲目標文件
OBJS = $(SRC:.c=.o)
# 鏈接爲可執行文件 $^ 代表所有的依賴文件
$(TARGET): $(OBJS)
[tab]$(CC) $^ -o $@ $(LDFLAGS) $(LDLIBS)
# 清除可執行文件和目標文件
clean:
[tab]rm -f $(OBJS)
[tab]rm -f $(TARGET)
# 編譯規則 加入頭文件 $@代表目標文件 $< 代表第一個依賴文件
%.o:%.c
[tab]$(CC) $(CFLAGS) $(INC) -o $@ -c $<
# 【1】編譯時加入了頭文件查找路徑。
# 【2】鏈接時加入了共享庫。
#【1】LDIBS指定共享庫,共享庫以 -l 爲前綴。
#【2】LDFLAGS指定共享庫查找路徑,路徑以 -L 爲前綴。