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
Abhijatya Singh
6628 silver badges23 bronze badges
1 Answer 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
Suedocode
2,5443 gold badges25 silver badges42 bronze badges
Sign up to request clarification or add additional context in comments.
main.dthat you don't think belong there?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?**literal?