Search code examples
makefile

-Wall flag not working from Makefile


When I run "g++ -Wall programName.cpp" the compiler outputs warnings about unused variables, but when I use the Makefile to compile, these warnings do not appear. I have the

OBJS = test.o

# Name of executable
NAME = ../test

# Flags to pass to the compiler. 
CFLAGS = -Wall

all: $(NAME)

$(NAME): $(OBJS)
   g++ $(CFLAGS) -o $(NAME) $(OBJS)

in my Makefile, but doesn't seem to work. Does anyone know what could be causing this???

When I run the make I get the output:

 g++    -c -o test.o test.cpp
 g++ -Wall -o test test.o

It seems like the -Wall is only being applied to the process of turning the .o file into an executable, not the part of .cpp -> .o


Solution

  • You are overriding the built-in rule for going from OBJS to NAME, but not the rule for compling .cpp to.o.

    The proper fix would appear to be to not override any built-in rules, and instead adding -Wall to the correct flags variable for the built-in rules (I guess in this case CXXFLAGS). See also Difference between CPPFLAGS and CXXFLAGS in GNU Make