3

I currently run make -j20 TGT=stuff that gets hundreds of flags from a Makefile that I cannot edit.

I want to remove optimization -O3 to pass -O0 to debug. Can I override what is in the Makefile by amending the make -j20 ... command ?

EDIT: I use g++

statquant
  • 13,672
  • 21
  • 91
  • 162
  • This depends on how the Makefile is written. Can you post relevant excerpts from the Makefile? – juppman Jul 21 '16 at 11:45
  • unfortunately no, but I can say what `CPPFLAGS` is set at – statquant Jul 21 '16 at 11:46
  • 1
    According to https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html the "last such option is the one that is effective" (regarding O-flags). Thus if you can figure out a way to add -O0 to a variable that is added later in the command than the -O3 flag then it should work. That is if you use gcc... – juppman Jul 21 '16 at 11:51
  • The easiest way may be to create an alias for the compile command used by the Makefile. I.e. cause `gcc ` to actually run `gcc -O0`. See http://stackoverflow.com/a/7131683/5533897 for creating such an alias in bash. Or simply make the gcc alias filter out `-O3`. – juppman Jul 21 '16 at 12:03

1 Answers1

3

The answer to your question depends on the rule that is used for compiling the C++ files. Since you do not want to share your makefile I explain this with the default implicit rule for C++ programs.

You can override any make variable, say 'MY_VAR' by calling make like this: make MY_VAR='Hello'

The default recipe for compiling C++ looks similar to this:

%.o : %.cpp
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@

CPPFLAGS are by convention the flags passed to the C and C++ preprocessor, CXXFLAGS are relevant for C++ compilation.

So I presume you makefile sets CXXFLAGS, lets say like this:

CXXFLAGS = -O3

You want it to be -O0, so you can override CXXFLAGS.

Call make like this:

make -j20 TGT=stuff CXXFLAGS='-O0'

Since you want to debug you probably should use:

make -j20 TGT=stuff CXXFLAGS='-O0 -g'

If the default for CXXFLAGS from your makefile includes other important flags you would have to specify those too - you override its value.

See also: https://www.gnu.org/software/make/manual/html_node/Overriding.html

The make manual is really good, I can only recommend having a look.

PaulR
  • 3,587
  • 14
  • 24