How to include clean target in Makefile?

后端 未结 3 777
面向向阳花
面向向阳花 2020-12-29 05:02

I have a Makefile that looks like this

CXX = g++ -O2 -Wall

all: code1 code2

code1: code1.cc utilities.cc
   $(CXX) $^ -o $@

code2: code2.cc utilities.cc
          


        
3条回答
  •  甜味超标
    2020-12-29 05:40

    The best thing is probably to create a variable that holds your binaries:

    binaries=code1 code2
    

    Then use that in the all-target, to avoid repeating:

    all: clean $(binaries)
    

    Now, you can use this with the clean-target, too, and just add some globs to catch object files and stuff:

    .PHONY: clean
    
    clean:
        rm -f $(binaries) *.o
    

    Note use of the .PHONY to make clean a pseudo-target. This is a GNU make feature, so if you need to be portable to other make implementations, don't use it.

提交回复
热议问题