Reputation:
My issue is that when running "make clean," it will not remove any of the files including the precompiled header files in the m directory (not sure of other directories as they did not have any files to remove). However, it does remove all of the files in the root directory where the makefile is. Note, M, D, N, and G are all subdirectories of the directory that the makefile is in. Futhermore, I tried manually running each of the terminal commands specified for the clean and it successfully removed all of the files in the m directory and root directory with no errors. I don't know why there is an inconsistency, any help is appreciated. Thanks in advance.
Shown below is my makefile:
#Compiler Variable
CMP=g++
#Compiler Flags Variable
CPPFLAGS= -c -std=c++17
#Directory Variables
m := ./M
ds := ./D
nn := ./N
g := ./G
M_HEAD = $(m)/L.h $(m)/M.h $(m)/T.h
M_SRC = $(m)/L.cpp $(m)/M.cpp $(m)/T.cpp
M_OJBS = L.o M.o T.o
#Targets and Dependencies
all: main.o m.o
$(CMP) main.o $(M_OJBS) -o test
main.o: main.cpp
$(CMP) $(CPPFLAGS) main.cpp
m.o: $(M_HEAD) $(M_SRC)
$(CMP) $(CPPFLAGS) $(M_HEAD) $(M_SRC)
clean:
rm -rf *.o *.h.gch test
cd $(m)
rm -rf *.o *.h.gch test
cd ..
cd $(ds)
rm -rf *.o *.h.gch test
cd ..
cd $(nn)
rm -rf *.o *.h.gch test
cd ..
cd $(g)
rm -rf *.o *.h.gch test
Upvotes: 0
Views: 1354
Reputation: 100856
When make runs a recipe, it runs each line of the recipe in its own shell. After that line of the recipe finishes, then the shell exits and a new shell is started for the next line.
So your clean
rule is the same as running:
/bin/sh -c "rm -rf *.o *.h.gch test"
/bin/sh -c "cd $(m)"
/bin/sh -c "rm -rf *.o *.h.gch test"
/bin/sh -c "cd .."
/bin/sh -c "cd $(ds)"
/bin/sh -c "rm -rf *.o *.h.gch test"
/bin/sh -c "cd .."
/bin/sh -c "cd $(nn)"
/bin/sh -c "rm -rf *.o *.h.gch test"
/bin/sh -c "cd .."
/bin/sh -c "cd $(g)"
/bin/sh -c "rm -rf *.o *.h.gch test"
So your cd
commands are useless because you change directories in a subshell, then the shell exits and the change of directories is lost, then you run the rm
command in the same directory you started in.
You have to put the cd
in the same (logical) line as the rm
, and you don't need to run cd ..
because it's implied:
clean:
rm -rf *.o *.h.gch test
cd $(m) && rm -rf *.o *.h.gch test
cd $(ds) && rm -rf *.o *.h.gch test
cd $(nn) && rm -rf *.o *.h.gch test
cd $(g) && rm -rf *.o *.h.gch test
Upvotes: 3
Reputation: 56
The solution may lie in the answer to this question:
How do I write the 'cd' command in a makefile?
The short answer is: you need to put semi-colons at the end of each of the lines in your clean dependency.
Upvotes: 1