目录
1. 适配多文件多目录
####################################################
#####Copyright (c) 2019, code farmer from sust.#####
###############All rights reserved.#################
.PHONY: clean install
# multi file
INC = ./include
SRC = ./src
OBJ = ./obj
BIN = ./bin
BIN_TARGET = $(BIN)/test
CC = gcc
CFLAGS = -g -Wall -I$(INC)
# match *.c
SOURCE = $(wildcard $(SRC)/*.c)
# replace *.o with *.c
OBJECT = $(patsubst %.c, $(OBJ)/%.o, $(notdir $(SOURCE)))
$(BIN_TARGET):$(OBJECT)
$(CC) $(CFLAGS) $(OBJECT) -o $@
$(OBJ)/%.o:$(SRC)/%.c
$(CC) $(CFLAGS) -c $< -o $@
# echo debug info
echo:
@echo $(CC)
@echo $(SRC)
@echo $(INC)
@echo $(OBJ)
@echo $(BIN)
install:
install -m 755 $(BIN) /usr/local/bin
clean:
-rm -rf $(OBJECT) $(BIN_TARGET)
2. 适配单个文件(C)
####################################################
#####Copyright (c) 2019, code farmer from sust.#####
###############All rights reserved.#################
.PHONY: clean install ALL
CC = gcc
#CFLAGS = -g -Wall -fno-stack-protector
CFLAGS = -g -Wall -DDEBUG
# match *.c
SOURCE = $(wildcard ./*.c)
# replace * with *.c
#BIN = $(patsubst %.c, %, $(notdir $(SOURCE)))
BIN = $(basename $(SOURCE))
ALL: $(BIN)
%: %.c
$(CC) $(CFLAGS) $< -o $@
# echo debug info
echo:
@echo $(CC)
@echo $(SOURCE)
@echo $(BIN)
install:
install -m 755 $(BIN) /usr/local/bin
clean:
-rm -rf $(BIN)
3. 适配单个文件(C++)
####################################################
#####Copyright (c) 2019, code farmer from sust.#####
###############All rights reserved.#################
.PHONY: clean install ALL
CC = g++
CFLAGS = -g -Wall -std=c++11
#CFLAGS = -g -Wall -Wno-int-to-pointer-cast -Wpointer-to-int-cast -fno-stack-protector
# for std::thread
#THREAD_FLAGS = -Wl,--no-as-needed -pthread
# match *.c*
SOURCE = $(wildcard ./*.c*)
BIN = $(basename $(SOURCE))
ALL: $(BIN)
%: %.c*
$(CC) $(CFLAGS) $(THREAD_FLAGS) $< -o $@
# echo debug info
echo:
@echo $(CC)
@echo $(SOURCE)
@echo $(SUFFIX)
@echo $(BIN)
install:
install -m 755 $(BIN) /usr/local/bin
clean:
-rm -rf $(BIN)