简体   繁体   中英

Makefile with multiple targets and automatic dependency generation using g++ -MMD

Another question on SO describes a wonderfully elegant makefile for a single target:

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)

My question is: how can we adapt this for multiple targets?

Your solution looks good, but if there are many targets this is a tiny bit easier to scale:

EXECS = exec_x exec_y

exec_x: a.o b.o c.o
exec_y: d.o e.o f.o

$(EXECS):
    ${LINK}

I've hacked together a solution which appears to work but I'd love to know if this is the appropriate technique.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM