简体   繁体   English

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

[英]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. 我有一个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 

I have specified -std=c++11 as CFLAGS but when I type make it doesn't seem to take that option. 我已将-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

Why is this happening. 为什么会这样呢?

I suggest using CXXFLAGS instead of CFLAGS . 我建议使用CXXFLAGS代替CFLAGS

CFLAGS is for C flags while CXXFLAGS is for C++ flags. CFLAGS用于C标志,而CXXFLAGS用于C ++标志。

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) . 然后,将使用$(CC) $(CFLAS)所有位置更改为$(CXX) $(CXXFLAGS)

This is happening because you are misusing Make. 发生这种情况是因为您滥用了Make。

For C++, use the correct variables, as documented on the GNU Make page : CXXFLAGS . 对于C ++,请使用GNU Make页面上记录的正确变量: 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. 如果这样做没有帮助,请制作一个最小,完整且可验证的示例 ,其中还会显示代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM