There is the following hierarchy in the working directory:

config (directory) main.cpp Makefile 

Makefile contents in working directory:

 main: main.o g++ -Wall -std=c++1y main.o ./config/load_config.o -o main main.o: config/load_config.o g++ -Wall -std=c++1y -o main.o -c main.cpp clean: rm *.o 

in the config directory is:

 load_config.cpp load_config.h Makefile 

config / Makefile content

 load_config.o: load_config.cpp g++ -Wall -std=c++1y -o load_config.o -c load_config.cpp clean: rm *.o 

if the working directory is config, then config / Makefile runs without errors. As a result, we have the file config / load_config.o . Make cd ../ After that, run make in the main directory of the program is going fine. But if there is no config / load_config.o , then the program is no longer going. The question is how to change the main Makefile so that it first executes make in the config folder ( build config / load_config.o )

    1 answer 1

    I would do something like this:

     main: main.o config/load_config.o g++ -Wall -std=c++1y $^ -o $@ main.o: main.cpp g++ -Wall -std=c++1y -o $@ -c $< config/load_config.o: $(MAKE) -C $(@D) $(@F) clean: rm *.o 

    explanation:

    • $^ - list of all prerequisites
    • $< - first prerequisite
    • $@ - rule target
    • $(MAKE) -C каталог цель - one of the recommended ways to call the gnu / make program recursively in the directory
    • $(@D) - when the target is represented as a каталог/файл , returns the каталог
    • $(@F) - when the target is represented as a каталог/файл , returns the файл
    • main.o: config / load_config.o and this line says what? Is it a file dependency or rule dependency? - kent_tompson
    • main.o: config/load_config.o : as in any rule, it is the dependency of the target (in this case, main.o ) on the prerequisite ( config/load_config.o ). I have changed your rule a little bit, because, in my opinion, it was illogical for you. - aleksandr barakin
    • in the main Makefile it was completely absent. I thought that this would run the rule from the Makefile in the config folder. That is, in the main Makefile you need to add targets for all dependencies from subdirectories. It seems to come. Thank. - kent_tompson