One make file to compile the entire source in the specified directory

I am currently working in a C ++ tutorial; I would like to have separate exercise folders in the book and one make file in the root, so that in the root directory I can print

make directoryName 

and it will compile all the sources in this directory and output the binary to the root directory. Here is what I still have:

 FLAGS= -Wall -Wextra -Wfloat-equal OUT=helloworld.out %: $(wildcard $@ /*.cpp) g++ $@ /$(wildcard *.cpp) -o $(OUT) $(FLAGS) 

But when I try to run it, all I get is

 pc-157-231:Section2$ make helloWorld make: `helloWorld' is up to date. 

Any help appreciated

Edit Note; the problem is not that I did not modify the target file; I did...

+5
source share
2 answers

Your problem is that in GNU auto mode, variables like $@ only have a value inside the body of the rule. From the GNU documentation.

[Automatic variables] cannot be accessed directly in the list of prerequisites for the rule. A common mistake is trying to use $@ in the prerequisite list; This will not work. ( source )

In addition, you do not need the $(wildcard ...) function in your rule (both in the body and in the list of prerequisites), although this is not an error:

Wildcard expansion occurs automatically in the rules. ( source )

+1
source

I wrote a make replacement that makes it very simple, but otherwise it is very experimental / not officially supported, etc. You can download it from https://github.com/dascandy/bob .

You could probably write this as:

 FLAGS= -Wall -Wextra -Wfloat-equal # Take all cpp files in a folder & compile it to executable with that folder name as name. (.*)/.*\.cpp => \1.out g++ -o $@ $^ $(FLAGS) 

or alternatively with intermediate objects (better for large projects, but probably useless for small exercises)

 FLAGS= -Wall -Wextra -Wfloat-equal # Read as, compile any CPP file to a file with the same root but .o as extension (.*)\.cpp => \1.o g++ -c -o $@ $^ $(FLAGS) # read as, take all objects in a subfolder together and compile into executable with folder name as name. (.*)/.*\.o => \1.out g++ -o $@ $^ 

The tool itself requires the installation of Boost.Filesystem.

If you want to have a default target that compiles all executables, add the following lines:

 .*.out => all echo -n 

(the echo is that he expects all the rules to have a command)

0
source

Source: https://habr.com/ru/post/1242186/


All Articles