liv2hak
liv2hak

Reputation: 14990

CFLAGS=-std=c++11 -O3 -Wall -pedantic not being recognized in makefile

I have a makefile where I compile a number of cpp files to make different executables.

CFLAGS=-std=c++11 -O3 -Wall -pedantic                                                                                                                                                                              

CC=g++1

LDFLAGS=

all: auto_variables explict_kw for_each functor member_initializer member_initializer2 reference ref_ptr smart_pointers vector

auto_variables : auto_variables.o
    $(CC) $(CFLAGS) auto_variables.o $(LDFLAGS) -o $@

explict_kw : explict_kw.o
    $(CC) $(CFLAGS) explict_kw.o $(LDFLAGS) -o $@

for_each : for_each o
    $(CC) $(CFLAGS) for_each.o $(LDFLAGS) -o $@

functor : functor.o
    $(CC) $(CFLAGS) functor.o $(LDFLAGS) -o $@

member_initializer : member_initializer.o
    $(CC) $(CFLAGS) member_initializer.o $(LDFLAGS) -o $@

member_initializer2 : member_initializer2.o
    $(CC) $(CFLAGS) member_initializer2.o $(LDFLAGS) -o $@

reference : reference.o
    $(CC) $(CFLAGS) reference.o $(LDFLAGS) -o $@

ref_ptr : ref_ptr.o
    $(CC) $(CFLAGS) ref_ptr.o $(LDFLAGS) -o $@

smart_pointers : smart_pointers.o
    $(CC) $(CFLAGS) smart_pointers.o $(LDFLAGS) -o $@

vector : vector.o
    $(CC) $(CFLAGS) vector.o $(LDFLAGS) -o $@

clean:
    rm -rf auto_variables explict_kw for_each functor member_initializer member_initializer2 reference ref_ptr smart_pointers vector 
    rm -rf *.o 

I have specified -std=c++11 as CFLAGS but when I type make it doesn't seem to take that option.

g++    -c -o auto_variables.o auto_variables.cpp
auto_variables.cpp:8:27: error: ISO C++ forbids declaration of ‘sum’ with no type [-fpermissive]
 auto sum(int x, int y) -> int
                           ^
auto_variables.cpp:8:27: error: top-level declaration of ‘sum’ specifies ‘auto’
auto_variables.cpp:8:27: error: trailing return type only available with -std=c++11 or -std=gnu++11
auto_variables.cpp: In function ‘int main()’:
auto_variables.cpp:16:8: error: ‘var_1’ does not name a type
   auto var_1 = 5;
        ^
auto_variables.cpp:18:8: error: ‘var_2’ does not name a type
   auto var_2 = 'c';
        ^
auto_variables.cpp:20:16: error: ‘var_1’ was not declared in this scope
   std::cout << var_1 << std::endl;
                ^
auto_variables.cpp:21:16: error: ‘var_2’ was not declared in this scope
   std::cout << var_2 << std::endl;
                ^
auto_variables.cpp:23:8: error: ‘fun_sum’ does not name a type
   auto fun_sum = [](int a, int b)
        ^
auto_variables.cpp:46:8: error: ‘itr’ does not name a type
   auto itr = mapofStrs.begin();
        ^
auto_variables.cpp:47:9: error: ‘itr’ was not declared in this scope
   while(itr != mapofStrs.end())
         ^
<builtin>: recipe for target 'auto_variables.o' failed
make: *** [auto_variables.o] Error 1

Why is this happening.

Upvotes: 0

Views: 700

Answers (2)

user1812457
user1812457

Reputation:

This is happening because you are misusing Make.

For C++, use the correct variables, as documented on the GNU Make page: CXXFLAGS.

CXXFLAGS=-std=c++11 -O3 -Wall -pedantic

You shouldn't need to set anything else at least at first.

This will make sure that you are using the correct implicit rules. If this doesn't help, make a Minimal, complete, and verifiable example which shows the code, too.

Upvotes: 1

R Sahu
R Sahu

Reputation: 206607

I suggest using CXXFLAGS instead of CFLAGS.

CFLAGS is for C flags while CXXFLAGS is for C++ flags.

CXXFLAGS=-std=c++11 -O3 -Wall -pedantic

Also, it's not clear to me what you meant by:

CC=g++1

Is that a typo? I suspect you meant to use

CC=g++

Keeping with the previous change, you should change that to:

CXX=g++

Then, change all the places where you are using $(CC) $(CFLAS) to $(CXX) $(CXXFLAGS).

Upvotes: 3

Related Questions