一个简单makefile(指定obj文件位置)

1. 可以指定 .o 文件生成的位置

2. 可以自动搜索原代码文件

 

用于例子的目录和文件结构大致如下:
app
    src
        obj
            test.o
            a.o
        a.cpp
        test.cpp
        makefile
    include
    bin

 

#make file v1.0

#define charset
export LC_ALL  = zh_CN.GBK
export LANG    = zh_CN.GBK

#define gcc global variable
LIBS = -lpthread -lrt
INCLUDE = -I../
CC = g++
CFLAGS += -Wno-deprecated -Wall -g

#define target file
BIN = ../bin/test

#define compile variable
CUR_PATH = ./
OBJ_PATH = $(CUR_PATH)obj/
#获取到本目录下所有cpp
ALL_CPP = $(foreach dir,$(CUR_PATH),$(wildcard $(dir)*.cpp))
#构造预期的目标 .o 文件
ALL_OBJ = $(patsubst $(CUR_PATH)%.cpp,%.o,$(ALL_CPP))
#指定 .o 文件生成的目录
ALL_OBJ_POS = $(addprefix $(OBJ_PATH),$(ALL_OBJ))

#define target
default:
        @echo "make file example v1.0"
        @echo " use make all"
        @echo $(DEPE)
all: $(ALL_OBJ)
#.PHONY : all
        $(CC) $(LIBS) $(CFLAGS) -o $(BIN) $(ALL_OBJ_POS)

$(ALL_OBJ): %.o: %.cpp
        $(CC) $(INCLUDE) $(CFLAGS) -c $< -o $(OBJ_PATH)$@

clean :
        -rm $(OBJ_PATH)*.o $(BIN) *.o

你可能感兴趣的:(C++,技术)