Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

g++ Flag variable not seen by make

Folks, I need some help in understanding what’s happening, I do not see how to pass an option to g++ compiler in a makefile. If you can just try to reproduce my stuff and get me some info, it would be valuable (for me).

Makefile content:

MYCPPFLAGS := what a surprise

%.o: %.cpp %.h
    g++ $(MYCPPFLAGS) -c $< -o $@

dumpy:
    echo $(MYCPPFLAGS)

It’s not that kind of magic, isn’t it???
Now I have a c++11 file named dessiner.cpp, if you need it I’ll post it, but let’s skip it for the moment. Please have a look at what I get in the terminal:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

grenx08{lc177705}hw-m2-cpp-07-dessiner: make dumpy
echo what a surprise
what a surprise
grenx08{lc177705}hw-m2-cpp-07-dessiner: make dessiner.o
g++    -c -o dessiner.o dessiner.cpp
dessiner.cpp:18:13: warning: override controls (override/final) only available with -std=c++11 or -std=gnu++11 [enabled by default]

[... 
a series of warnings/ errors because I did not use std=c++11 option] 

make: *** [dessiner.o] Error 1

So my question is : why can’t I see anything about "what a surprise" in the g++ command-line, but just four blank spaces???? Is make smarter than me (LoL, of course!!!) and erasing stupid options passed to g++??? The real issue is that in this case, I cannot even give the good options…
Please find hereby the sware versions, quite old stuff due to a constrained working environment. Any hint appreciated.

grenx08{lc177705}hw-m2-cpp-07-dessiner: make -v
GNU Make 3.82
Built for x86_64-redhat-linux-gnu
Copyright (C) 2010  Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
grenx08{lc177705}hw-m2-cpp-07-dessiner: g++ --version
g++ (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44)
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Edit 1:

grenx08{lc177705}hw-m2-cpp-07-dessiner: make -d dessiner.o
GNU Make 3.82
Built for x86_64-redhat-linux-gnu
[ tons of output ]
  No need to remake target `dessiner.cpp'.
 Finished prerequisites of target file `dessiner.o'.
Must remake target `dessiner.o'.
Invoking builtin recipe to update target `dessiner.o'.
g++    -c -o dessiner.o dessiner.cpp
[ other irrelevant output ]

grenx08{lc177705}hw-m2-cpp-07-dessiner: make -r dessiner.o
make: *** No rule to make target `dessiner.o'.  Stop.

So it looks as my rule definition has a flaw? I have no .h file…

>Solution :

This recipe:

%.o: %.cpp %.h
        g++ $(MYCPPFLAGS) -c $< -o $@

tells make "hey make, if you’re trying to build a target that matches the pattern %.o, and if you have prerequisites that match the patterns %.cpp and %.h, then you can use this recipe to build the target".

Note carefully: ALL the prerequisites MUST exist, or there must be rules telling make how to build them, otherwise this rule does not match.

In your case, you have a file dessiner.cpp but you do not have a file dessiner.h, and so make cannot create both the prerequisites, and so your rule doesn’t match and make ignores it.

Then make looks in its built-in set of rules and it sees a %.o : %.cpp pattern rule there, and since that prerequisite exists, it matches and make runs that recipe. That recipe doesn’t know anything about your variable MYCPPFLAGS so it’s not used.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading