Phidelux
Phidelux

Reputation: 2271

CFLAGS are ignored in Makefile

I am using the following makefile to build my project:

CC      = /usr/bin/g++
CFLAGS  = -Wall -pedantic -std=c++0x
LDFLAGS = 

OBJ = main.o pnmhandler.o pixmap.o color.o 

pnm: $(OBJ)
    $(CC) $(CFLAGS) -o pnm $(OBJ) $(LDFLAGS)

%.o: %.c
    $(CC) $(CFLAGS) -c $<

As I run make I get the following error:

/usr/include/c++/4.9.1/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.

As I can read from the following line, the CFLAGS are not properly included, but I have no idea what I am doing wrong:

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

Also tried -std=c++11 and -std=gnu++11, without any results. Any ideas?

If I run make -Bn, I get:

g++    -c -o main.o main.cpp
g++    -c -o pnmhandler.o pnmhandler.cpp
g++    -c -o pixmap.o pixmap.cpp
g++    -c -o color.o color.cpp
/usr/bin/g++ -Wall -pedantic -std=c++0x -o pnm main.o pnmhandler.o pixmap.o color.o  

EDIT: Replacing the rule %.o: %.c with %.o: %.cpp fixes my problem.

Upvotes: 4

Views: 5776

Answers (1)

Toby Speight
Toby Speight

Reputation: 30932

The reason you see

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

is that Make is invoking its standard rule to create the object file:

%.o: %.cpp
#  recipe to execute (built-in):
        $(COMPILE.cpp) $(OUTPUT_OPTION) $<

The command expands to

$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c -o $@ $<

Instead of setting CC and CFLAGS in your makefile, you should set CXX and CXXFLAGS, which are meant for C++ rather than C. That allows the built-in rule above to work for you, and then you just need to make sure the right linker is used, e.g. with

pnm: LINK.o=$(LINK.cc)
pnm: $(OBJ)

You also don't need the %.o: %.c rule, as you have no C sources.


Complete Makefile:

CXX      = /usr/bin/g++
CXXFLAGS = -Wall -pedantic -std=c++0x 

OBJ = main.o pnmhandler.o pixmap.o color.o 

pnm: LINK.o=$(LINK.cc)
pnm: $(OBJ)

clean::
    $(RM) pnm

.PHONY: clean

Upvotes: 7

Related Questions