简体   繁体   中英

Is it possible to use gcc/g++/nvcc automatic dependency -M in a single pass of a Makefile without saving dependencies to a file?

I was trying to come up with a solution for automatic dependency using gcc/g++/nvcc and a Makefile.

I thought I'd come up with a solution, to call gcc -M $(SRC FILES) in a Makefile before any compilation targets, with the assumption that Make would now have updated rules for the compilation targets.

An example of the Makefile I've thought would work is as follows:

PROG = main.out

SRC = $(wildcard *.cc)
OBJ = $(SRC:.cc=.o)

all: $(PROG) | deps

$(PROG): $(OBJ)
    g++ -o $@ $^

$(OBJ): $(SRC)
    g++ -c $<

.PHONY: deps

deps:
    g++ -M $(SRC)

Now I'm wondering if the call to

    g++ -M $(SRC)

Just causes the dependencies to be printed to stdout and infact the Makefile is still none the wiser to the automatic dependencies.

Ideally I'm looking for a solution that will run in a single pass of a Makefile and use gcc/g++/nvcc automatic dependency flags, and preferably one that doesn't require saving the dependencies to a whole bunch of files.

You can do something like below to get both .o and .d files:

g++ -c main.cpp -o main.o -MP -MMD -MF main.d

So define your dependency files (eg DEPFILES ) in your Makefile and generate .d like the above command, then include DEPFILES by - , which tells GNU Make to include the dep file if it exists.

-include $(DEPFILES)

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