0

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?

asked Jan 19, 2015 at 14:23
9
  • What contents are in main.d that you don't think belong there? Commented Jan 19, 2015 at 14:25
  • main.o main.d : main.cpp main.h \ ** /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/iostream \ /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/x86_64-redhat-linux/bits/c++config.h \ /usr/include/bits/wordsize.h \ /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/x86_64-redhat-linux/bits/os_defines.h \ /usr/include/features.h /usr/include/sys/cdefs.h \** Ideally it should show only first line (main.o main.d : main.cpp main.h) . Why the remaining lines are comming and the output expected was main.o: main.cpp main.h? Commented Jan 19, 2015 at 14:35
  • Your code is clearly including some of them as well (and they are including the other headers). The prerequisites can't stop at the first-level included header files or they will miss things. Is that ** literal? Commented Jan 19, 2015 at 14:39
  • Then how to get the desired output? Commented Jan 19, 2015 at 14:40
  • Do it manually if you really care about not having the extra information there. But the point is that extra information is correct and you don't ever need to care about it. Commented Jan 19, 2015 at 14:48

1 Answer 1

1

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*
answered Jan 20, 2015 at 19:14
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.