Gnu Makefile - Handling dependencies

后端 未结 12 2508
滥情空心
滥情空心 2021-02-07 20:24

What approach do C++ programmers on Unix platform use to create and manage Makefiles?

I was using hand made Makefiles for my projects but they don\'t handle header file

12条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-07 21:06

    I top tip that I have found useful when building dependency files is to include the dependency file as a target in the generated rule:

    file.d file.o : file.c header.h header2.h ...
    

    Thus make will regenerate the dependencies if the source or any of the headers change. Including phony targets for the headers (GCC -MP) should then allow stable builds when headers are removed - the absense of required header remains a compilation error, not a make dependency error.

    Assuming that dependency files are generated into the same directory as the object files, the following should work for GCC on Unix:

    -include $(OBJ:.o=.d)
    
    $(OBJDIR)/%d : $(SRCDIR)/%.cpp
            mkdir -p $(@D)
            echo -n "$@ " > $@.tmp
            $(CXX) $(CPPFLAGS) -MM -MP -MT $(@:.d=.o) $< >> $@.tmp
            mv $@.tmp $@
    

    (from memory)

提交回复
热议问题