CFLAGS = -std = c ++ 11 -O3 -Wall -pedantic在makefile中无法识别

时间:2016-02-15 21:07:58

标签: c++ c++11 makefile

我有一个makefile,我编译了许多cpp文件以生成不同的可执行文件。

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 

我已将-std=c++11指定为CFLAGS但是当我输入make时,它似乎没有采用该选项。

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

为什么会这样。

2 个答案:

答案 0 :(得分:3)

我建议使用CXXFLAGS代替CFLAGS

CFLAGS用于C标志,而CXXFLAGS用于C ++标志。

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

另外,我不清楚你的意思是什么:

CC=g++1
这是一个错字吗?我怀疑你打算使用

CC=g++

保持之前的更改,您应将其更改为:

CXX=g++

然后,将您使用$(CC) $(CFLAS)的所有地点更改为$(CXX) $(CXXFLAGS)

答案 1 :(得分:1)

这种情况正在发生,因为你误用了Make。

对于C ++,请使用正确的变量,如GNU Make pageCXXFLAGS中所述。

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

至少在开始时你不应该设置其他任何东西。

这将确保您使用正确的隐式规则。如果这没有帮助,请创建一个显示代码的Minimal, complete, and verifiable example