Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

It seems like make is ignoring my cflag options, as the compiler complains that I need to have -std=c++11 as a flag, but that option is included in my makefile.

CC=g++
# Flags for the C compiler
CXX_FLAGS= \
    -Wall \
    -std=c++11 \
# Linker Flags
LD_FLAGS= 
# Sources to compile
SOURCES=main.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXE=HolidayLights.out
all: $(SOURCES) $(EXE)
# Primary build target
$(EXE): $(OBJECTS)
    $(CC) $(LD_FLAGS) $(OBJECTS) -o $@
    $(CC) -c $(CXX_FLAGS) -o $@ $<

The output of the build commands of this makefile are:

g++     -c -o main.o main.cpp

I don't understand why it's not listing my flags in the output. Also, I know for a fact that it is ignoring -std=c++11 flag as it complains about non-static constant member, and it shouldn't with that flag enabled.

Edit0: Notation change

@RSahu your comment seems to be just a notation thing, however I did make the change and it will be easier to read down the road. – HSchmale Feb 2, 2015 at 4:32 CC and CC_FLAGS are for the C compiler. Use CXX and CXX_FLAGS for the C++ compiler. Or is it CXXFLAGS? I love Make. – n. m. could be an AI Feb 2, 2015 at 4:35 If you rely on the default rules used by GNU Make, you can just define CXXFLAGS and CFLAGS to control the compiler flags to get all you need. – R Sahu Feb 2, 2015 at 4:36 @n.m. It does make more sense to do it that way. I just based my makefiles after an old tutorial for C makefiles. I found online from some college. – HSchmale Feb 2, 2015 at 4:37

Your .o: rule is not being selected, change it to .cpp.o: instead.

By default (from the GNU make doco):

n.o is made automatically from n.cc, n.cpp, or n.C with a recipe of the form '$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c'

The reason why your rule is not working in the first case is that a single-suffix implicit rule is supposed to be the source type rather than the target type. Hence the rule would be .cpp: (again, from the make doco):

A double-suffix rule is defined by a pair of suffixes: the target suffix and the source suffix. It matches any file whose name ends with the target suffix. The corresponding implicit prerequisite is made by replacing the target suffix with the source suffix in the file name. A two-suffix rule whose target and source suffixes are '.o' and '.c' is equivalent to the pattern rule '%.o : %.c'.

A single-suffix rule is defined by a single suffix, which is the source suffix. It matches any file name, and the corresponding implicit prerequisite name is made by appending the source suffix. A single-suffix rule whose source suffix is '.c' is equivalent to the pattern rule '% : %.c'.

However, read that last sentence carefully. The .cpp: rule is only effective if you want to turn XYZZY.cpp into XYZZY. Since your target is of the form XYZZY.o, it won't be used.

Of course, the other alternative is to not touch the default rules at all, and instead just modify the variables they use:

CXX      = g++
CPPFLAGS = -Wall -std=c++11 -O2
LD_FLAGS =
SOURCES  = main.cpp
OBJECTS  = $(SOURCES:.cpp=.o)
EXE      = HolidayLights.out
all: $(SOURCES) $(EXE)
$(EXE): $(OBJECTS)
    $(CXX) $(LD_FLAGS) $(OBJECTS) -o $@

And, if you want to use implicit rules, you should really use the preferred form, which is the newer pattern rules. They're a lot more powerful than the suffix ones.

Now it makes a lot more sense. So basically make selects the rule to follow for generating object files based on the extension. – HSchmale Feb 2, 2015 at 5:08

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.