1

I have many makefiles under the same project directory.

The Makefile_prune:

OBJS    = main.o
SOURCE  = main.cpp
HEADER  =   Division_Euclidean_space.h  Find_diameter.h Find_k_max.h    Householder.h   IO.h    Mean_variance.h Point.h Random_generator.h  Random_kd_forest.h  Tree.h
OUT     =   rkd
CC  = g++
FLAGS   =   -std=c++0x  -O3 -c  -Wall
# -c flag generates object code for separate files

all: $(OBJS)
    $(CC)   $(OBJS) -o $(OUT)
    make clean

# create/compile the individual files >>separately<< 
main.o: main.cpp
    $(CC) $(FLAGS) main.cpp

.PHONY : all
# clean house
clean:
    rm -f $(OBJS)

# do a bit of accounting
count:
    wc $(SOURCE) $(HEADER)

I am getting the error for clean, as you can see below:

samaras@samaras-A15:~/code$ make -f Makefile_prune 
g++ -std=c++0x  -O3 -c  -Wall main.cpp
g++ main.o -o rkd
make clean
make[1]: Entering directory `/home/samaras/paper/rkd_forest/code'
make[1]: *** No rule to make target `clean'.  Stop.
make[1]: Leaving directory `/home/samaras/paper/rkd_forest/code'
make: *** [all] Error 2

Then I can run make -f Makefile_prune clean, which will execute the clean-up, but I want to be single commanded, I mean, not having to press enter twice.

Related answers:

  1. “make clean” results in “No rule to make target `clean'”
  2. gcc makefile error: “No rule to make target …”

Idea:

The makefile actually was used for a C project I had and I just made some changes to fit the C++, maybe this is the problem.

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305

2 Answers2

1

I think you need to call make -f Makefile_prune clean.

When calling make clean since you are not using the -f option it won't call Makefile_prune. Its calling some other file named 'Makefile' which may not have the clean target.

Also reference: https://www.gnu.org/software/make/manual/html_node/Makefile-Names.html

sleeping_dragon
  • 701
  • 1
  • 10
  • 27
1

Don't call make from inside your makefile if you don't have to. And in this case, you definitely don't:

all: $(OUT) clean

clean : $(OUT)
    rm -f $(OBJS)

$(OUT) : $(OBJS) 
    $(CC) $(OBJS) -o $(OUT)

This will definitely be faster since you don't have to shell out. Also, you won't have to rebuild rkd if you run make again. Although, if you want to rebuild it everytime, substitute $(OUT) with some PHONY rule (e.g. clean : build, and build : $(OBJS))

Also, clean and count should be PHONY:

.PHONY : all clean count

Lastly, one thing worth pointing out. You don't have to manually remove the *.o files. make should remove them for you. See Chained Rules.

Barry
  • 286,269
  • 29
  • 621
  • 977
  • Nice answer too, however for the chronological order, I will keep the other one as the selected one, +1! – gsamaras Dec 15 '14 at 00:41