Makefile脚本例子

1. 

all: student.o main.o
	g++ student.o main.o -o bin
 
 
student.o: student.cpp student.h
	g++ -c student.cpp 
main.o: main.cpp student.h
	g++ -c main.cpp 
 
clean:
	rm -rf *.o

2.

CC = g++ 
TAG = bin_test

$(TAG):  main.o student.o
	$(CC) $^ -o  $(TAG)

main.o: main.cpp
	$(CC) -c main.cpp

student.o: student.h student.cpp
	$(CC) -c student.cpp

clean: 
	rm -rf *.o
	rm -rf $(TAG)	

3.编译指定目录的.h和.cpp文件(cpp文件引用.h文件也要指定目录:如:#include"../include/student.h");

CC = g++
OBJECTS = main.o student.o 
TAG = test_bin
	

$(TAG): $(OBJECTS)
	$(CC) $^  -o $(TAG) 

main.o:  ./project/main.cpp
	$(CC) -c ./project/main.cpp
	

student.o:  ./include/student.h ./project/student.cpp 
	$(CC) -c ./project/student.cpp 

clean:
	rm -rf $(OBJECTS)
	rm -rf $(TAG)
	

(4)在(3)的基础上修改,嵌入shell命令,把.o文件、可执行文件编译到指定目录。

CC = g++
OBJECTS = main.o student.o 
TAG = test_bin
	

$(TAG): $(OBJECTS)
	$(CC) ../build/main.o  ../build/student.o  -o $(TAG) 
	mv $(TAG) ../bin/

main.o:  ./project/main.cpp
	$(CC) -c ./project/main.cpp
	mv *.o ../build/
	

student.o:  ./include/student.h ./project/student.cpp 
	$(CC) -c ./project/student.cpp 
	mv *.o ../build/

clean:
	rm -rf ../bin/$(TAG)
	rm -rf ../build/*.o

(5)与(4)内容一样,区别是makefile 放到了,(4)的上级目录。这里遇到一个错(CURRENT_DIR = $(shell pwd),语句后多了一个空格,报错一直没看出来,后来输出才发现@echo)

CC = g++
OBJECTS = main.o student.o 
TAG = test_bin
CURRENT_DIR = $(shell pwd)
BUILD_DIR = $(CURRENT_DIR)/build
BIN_DIR = $(CURRENT_DIR)/bin
INCLUDE_DIR = $(CURRENT_DIR)/src/include
PROJECT_DIR = $(CURRENT_DIR)/src/project

$(TAG): $(OBJECTS)
	$(CC) $(BUILD_DIR)/main.o  $(BUILD_DIR)/student.o  -o $(TAG) 
	mv $(TAG) $(BIN_DIR)

main.o:  $(PROJECT_DIR)/main.cpp
	$(CC) -c $(PROJECT_DIR)/main.cpp
	mv *.o $(BUILD_DIR)
	
student.o:  $(INCLUDE_DIR)/student.h $(PROJECT_DIR)/student.cpp 
	$(CC) -c $(PROJECT_DIR)/student.cpp 
	mv *.o  $(BUILD_DIR)

clean:
	rm -rf  $(BIN_DIR)/$(TAG)
	rm -rf $(BUILD_DIR)/*.o

show:
	 ls $(BIN_DIR)
	 ls $(BUILD_DIR)

(6) 在(5)的基础上完善,make clean 会删除目录。注意点:CHECK 作为依赖要放在前面,先建目录,否则 mv .o 时会出问题

CC = g++
OBJECTS = main.o student.o 
TAG = test_bin
CURRENT_DIR = $(shell pwd)
BUILD_DIR = $(CURRENT_DIR)/build
BIN_DIR = $(CURRENT_DIR)/bin
INCLUDE_DIR = $(CURRENT_DIR)/src/include
PROJECT_DIR = $(CURRENT_DIR)/src/project

$(TAG): CHECK $(OBJECTS) 
	$(CC) $(BUILD_DIR)/main.o  $(BUILD_DIR)/student.o  -o $(TAG) 
	mv $(TAG) $(BIN_DIR)

main.o:  $(PROJECT_DIR)/main.cpp
	$(CC) -c $(PROJECT_DIR)/main.cpp
	mv *.o $(BUILD_DIR)
	
student.o:  $(INCLUDE_DIR)/student.h $(PROJECT_DIR)/student.cpp 
	$(CC) -c $(PROJECT_DIR)/student.cpp 
	mv *.o  $(BUILD_DIR)

CHECK:
	mkdir -p $(BIN_DIR)  $(BUILD_DIR)

clean:
	rm -rf  $(BIN_DIR)
	rm -rf $(BUILD_DIR)

show:
	 ls $(BIN_DIR)
	 ls $(BUILD_DIR)

(7)技术点:a. shell脚本查找文件;b. makefile隐含规则(%.o:%.cpp);c. OBJ:= $(SRC:%.cpp=%.o)

思考点:

%.o:%.cpp $(DEPS)
    $(CC) -c $< -o $@

这个语句中,@echo $(%.cpp)是空的,makefile 如何知道%.cpp的位置的?

CC = g++
TAR = test.bin
SRC:=$(shell find ./ -name "*.cpp")
DEPS:=$(shell find ./ -name "*.h")
 
OBJ:= $(SRC:%.cpp=%.o)


$(TAR):$(OBJ)
	$(CC)  $(OBJ) -o $(TAR)

%.o:%.cpp $(DEPS)
	$(CC) -c $< -o $@

clean:
	rm -rf $(TAR) $(OBJ)

(8) 在(7)的基础上简化%.o:%.cpp $(DEPS) 可以去掉依赖头文件的条件

CC = g++
TAR = test.bin
SRC:=$(shell find ./ -name "*.cpp")
OBJ:= $(SRC:%.cpp=%.o)

$(TAR):$(OBJ)
	$(CC)  $(OBJ) -o $(TAR)

%.o:%.cpp 
	$(CC) -c $< -o $@

clean:
	rm -rf $(TAR) $(OBJ)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值