makefile实验五 make clean rebuild 以及规则中的模式替换. 综合小小实验

makefile代码:

.PHONY : rebuild clean $(TARGET)  #声明伪目标时,除直接使用目标名外, 也可以使用 $(变量)  这是取变量的值

CC := g++
TARGET := hello-makefile.out
OBJS := func.o main.o 

$(TARGET) : $(OBJS)
    $(CC) -o $@ $^

$(OBJS) : %.o : %.c
    $(CC) -o $@ -c $^

clean :
    $(RM)  $(OBJS) 
    $(RM)  $(TARGET)
    @echo "clean"

#.PHONY : rebuild clean $(TARGET)  #本代码的伪目标声明在这里,和写在上面实测都是可以的。都可以产生伪目标的效果。
#实测时,以make clean测试,无论本地是否存在clean文件,clean都是伪目标的效果。 rebuild : clean $(TARGET) @echo
"rebuild"

 

测试过程:

root@ubuntu:~/Makefile_Test/5make_test# ls
func.c  func.o  hello-makefile.out  main.c  main.o  makefile
root@ubuntu:~/Makefile_Test/5make_test# make clean
rm -f  func.o main.o  
rm -f  hello-makefile.out
clean
root@ubuntu:~/Makefile_Test/5make_test# ls
func.c  main.c  makefile

root@ubuntu:~/Makefile_Test/5make_test# make
g++ -o func.o -c func.c
g++ -o main.o -c main.c
g++ -o hello-makefile.out func.o main.o
root@ubuntu:~/Makefile_Test/5make_test# ls
func.c  func.o  hello-makefile.out  main.c  main.o  makefile

root@ubuntu:~/Makefile_Test/5make_test# make rebuild
rm -f  func.o main.o  
rm -f  hello-makefile.out
clean
g++ -o func.o -c func.c
g++ -o main.o -c main.c
g++ -o hello-makefile.out func.o main.o
rebuild
root@ubuntu:~/Makefile_Test/5make_test# ls
func.c  func.o  hello-makefile.out  main.c  main.o  makefile
root@ubuntu:~/Makefile_Test/5make_test# 

  root@ubuntu:~/Makefile_Test/5make_test# ./hello-makefile.out
  hello world
  main.c: a = 0
  root@ubuntu:~/Makefile_Test/5make_test#

 

源码:

 

posted @ 2019-08-15 17:13  一匹夫  阅读(1059)  评论(0编辑  收藏  举报