Search code examples
c++makefile

"clean" not working in make file


This is my make file.

 all: observer

    observer: main.o weather_center.o display.o subject.o observer.o
         g++ main.o weather_center.o display.o subject.o observer.o -o observer

    main.o: main.cpp
        g++ -c main.cpp

    weather_center.o: weather_center.cpp
        g++ -c weather_center.cpp

    display.o: display.cpp
        g++ -c display.cpp

    subject.o: subject.cpp
        g++ -c subject.cpp

    observer.o: observer.cpp
        g++ -c observer.cpp

    clean:
        rm -f  *o observer

Here I'm trying to use

clean:
        rm -f  *o observer

To clean up the temporary *.o files. But program compiles and generate the target assembly, but doesn't delete the *.o files. Not showing any errors also.

  • I tried rm -f *o observer in terminal. It works fine.
  • I have used Tab for indent
  • there are no files start with clean or rm in the directory.
  • tried $(RM) instead of rm. but no lucky

Solution

  • I found the issue. Have to specify clean as a target of all otherwise it wont call. generally like this.

    all: [your executive names] clean

    In above case

    all: observer clean

    Here is the full make file of above case

    all: observer clean
    
    observer: main.o weather_center.o display.o subject.o observer.o
        g++ main.o weather_center.o display.o subject.o observer.o -o observer
    
    main.o: main.cpp
        g++ -c main.cpp
    
    weather_center.o: weather_center.cpp
        g++ -c weather_center.cpp
    
    display.o: display.cpp
        g++ -c display.cpp
    
    subject.o: subject.cpp
        g++ -c subject.cpp
    
    observer.o: observer.cpp
        g++ -c observer.cpp
    
    clean:
        rm -f  *o observer