GNU Make C++. Linux

Perhaps not quite understanding what to look for, I can't find a valid example. The project is in C++, divided into subfolders (src: Engine{Component, Utils}, Process{Objects,..., etc}). How do I describe a MakeFile so that it collects sources by folder? From all that I scraped, I saw a variation of "each folder has a MakeFile", which was a little disorienting. If this is the same method, is there a need to repeat in each the instructions of the libraries used(Boost, for example), flags, etc.? In advance thanks.

1 answers

I saw a variation of "each folder has a MakeFile"

The presence of a separate file with instructions for the program gnu/make in each assembly directory is not always necessary, although it is probably convenient.

Instead of a thousand words, it is better to give a primitive example

Let there be such a file/directory structure:

$ tree
.
├── GNUmakefile
├── program.cpp
└── somepart
    └── somecode.cpp

1 directory, 3 files

The minimum required content of the GNUmakefile file for this case is just one line declaring that " for getting program requires program.o and somepart/somecode.o":

$ cat GNUmakefile
program: program.o somepart/somecode.o

We check which commands make will execute (the -n option, also known as --dry-run, only outputs a sequence of commands without executing them):

$ make -n
g++    -c -o program.o program.cpp
g++    -c -o somepart/somecode.o somepart/somecode.cpp
cc   program.o somepart/somecode.o   -o program

A little more about where the make program got all this from-in my other answer.


Need to add something to (for example) options for c++? use the appropriate one variable:

$ cat GNUmakefile
CXXFLAGS += -I/путь/к/boost
program: program.o somepart/somecode.o

Check:

$ make -n
g++ -I/путь/к/boost   -c -o program.o program.cpp
g++ -I/путь/к/boost   -c -o somepart/somecode.o somepart/somecode.cpp
cc   program.o somepart/somecode.o   -o program

If a separate file is still required, then it is usually called by a recipe of the form:

$(MAKE) -C подкаталог

In order for the called instance of the program gnu/make to "get" a variable, it must be explicitly exported using the export directive in the "main" file:

export CXXFLAGS += ...
 1
Author: aleksandr barakin, 2018-11-19 09:54:27