make: implicit rule to link c++ project

后端 未结 7 1026
礼貌的吻别
礼貌的吻别 2020-12-31 04:52

I am working my way through a make tutorial. Very simple test projects I am trying to build has only 3 files: ./src/main.cpp ./src/implementation.cpp and

7条回答
  •  情话喂你
    2020-12-31 05:15

    How about this for a minimal Makefile:

    SOURCES = src/main.cpp src/implementation.cpp
    
    CXX = g++
    CXXFLAGS = -g -W -Wall -Werror
    LDFLAGS = -g
    
    OBJECTS = $(SOURCES:.cpp=.o)
    
    prog: $(OBJECTS)
        $(CXX) $(LDFLAGS) -o $@ $^
    
    clean::
        $(RM) prog
    
    .cpp.o:
        $(CXX) -MD -MP $(CXXFLAGS) -o $@ -c $<
    
    clean::
        $(RM) src/*.o
    
    DEPENDS = $(SOURCES:.cpp=.d)
    
    -include $(DEPENDS)
    
    %.d:
        @touch $@
    
    clean::
        $(RM) src/*.d
    

    This assumes GNU make and gcc, but it adds proper dependency tracking, so there is no need to explicitly list the header file dependencies.

提交回复
热议问题