Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One makefile to compile all the source in a specified directory

Tags:

c++

makefile

I am currently working through a c++ textbook; I'd like to have separate folders for exercises in the book, and a single makefile in the root, so that in the root directory I can type

make directoryName

and it will compile all the sources in that directory, and output a binary into the root. Here is what I have so far:

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 haven't changed the target file; I did...

like image 955
Ben Wainwright Avatar asked Nov 08 '22 20:11

Ben Wainwright


1 Answers

Your problem is that automatic GNU make variables such as $@ only have a value within the body of the rule. From GNU make documentation.

[Automatic variables] cannot be accessed directly within the prerequisite list of a rule. A common mistake is attempting to use $@ within the prerequisites list; this will not work. (source)

Additonally, you do not need the $(wildcard ...) function in your rule (both in body and in prerequisite list), although it's not a mistake either:

Wildcard expansion happens automatically in rules. (source)

like image 129
kfx Avatar answered Nov 14 '22 23:11

kfx