Abhijatya Singh
Abhijatya Singh

Reputation: 642

Makefile auto-dependency generation

I am trying to see auto generated dependency the makefile is below:

  OBJS := main.o 

  run : $(OBJS)
        $(CC) $(OBJS) -o run -lstdc++

  -include $(OBJS:.o=.d)

 %.o : %.cpp
      $(CC) -c $(CFLAGS) $*.cpp -o $*.o

 %.d : %.cpp
       @set -e; rm -f $@; \
       $(CC) -M $(CPPFLAGS) $< > $@.$$$$; \
       echo "creating dependency file."; \
       sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
       rm -f $@.$$$$

  # remove compilation products
  clean :
         rm -f run *.o *.d*

But the makefile generate a huge list of dependency in main.d but the actual needed is the first few lines. So where I am getting wrong?

Upvotes: 0

Views: 126

Answers (1)

Suedocode
Suedocode

Reputation: 2534

Try this instead:

  OBJS := main.o 

  run : $(OBJS)
        $(CC) $(OBJS) -o run -lstdc++

  -include $(OBJS:.o=.d)

 %.o : %.cpp
      $(CC) -c -MMD -MP $(CFLAGS) $*.cpp -o $*.o

  # remove compilation products
  clean :
         rm -f run *.o *.d*

Upvotes: 1

Related Questions