【问题标题】:Makefile with multiple targets and automatic dependency generation using g++ -MMD具有多个目标的 Makefile 和使用 g++ -MMD 自动生成依赖项
【发布时间】:2012-12-09 02:52:07
【问题描述】:

Another question on SO 为单个目标描述了一个非常优雅的 makefile:

CXX = g++                     # compiler
CXXFLAGS = -g -Wall -MMD      # compiler flags
OBJECTS = x.o y.o z.o         # object files forming executable
DEPENDS = ${OBJECTS:.o=.d}    # substitutes ".o" with ".d"
EXEC = a.out                  # executable name

${EXEC} : ${OBJECTS}          # link step
    ${CXX} ${OBJECTS} -o ${EXEC}

-include ${DEPENDS}           # copies files x.d, y.d, z.d (if they exist)

我的问题是:我们如何针对多个目标进行调整?

【问题讨论】:

    标签: gcc makefile dependencies


    【解决方案1】:

    您的解决方案看起来不错,但如果有很多目标,这会更容易扩展:

    EXECS = exec_x exec_y
    
    exec_x: a.o b.o c.o
    exec_y: d.o e.o f.o
    
    $(EXECS):
        ${LINK}
    

    【讨论】:

      【解决方案2】:

      我已经组合了一个似乎可行的解决方案,但我很想知道这是否是合适的技术。

      CXX = g++                     # compiler
      CXXFLAGS = -g -Wall -MMD      # compiler flags
      LINK = ${CXX} $^ -o $@        # link step
      
      exec_x: a.o b.o c.o
          ${LINK}
      
      exec_y: d.o e.o f.o
          ${LINK}
      
      -include *.d
      

      【讨论】:

        猜你喜欢
        • 2012-08-05
        • 2012-02-08
        • 2017-04-11
        • 2023-04-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-20
        相关资源
        最近更新 更多