简体   繁体   中英

How to hide g++ c++11 compile warnings in ubuntu 12.10

When I execute

g++ main.cpp 

I get the output

main.cpp:9:17: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]
main.cpp:10:15: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]
main.cpp:11:16: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]
main.cpp:12:14: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]
main.cpp:13:13: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]

Now if I run

g++ -std=c++11 main.cpp

The warnings go away but I'd like to not have to explicitly specify something already enabled. I'm hesitant to make a simple bash alias because if I remember right gcc is already an alias of sorts. Is it safe to do it anyways or is there a correct way to fix this in a config file or something? I'm running on Ubuntu 12.10 x64 with default repositories.

"I'd like to not have to explicitly specify something already enabled" - actually that's almost certain to be the least-energy-expenditure method. There will be a host of other possibilities ranging from piping the output through grep -v to recoding and recompiling the compiler toolchain but they will most likely all be harder than just adding that flag to your command line.

And, if you're worried about the extra characters and possible alias conflicts, you can always call your alias g11 or something similar. Then all you have to remember is to use the right name but any organism with a spinal column should be sufficiently advanced enough to handle that :-)

GCC编译器系列(gcc,g ++等)接受禁用所有警告的-w开关 - 但你不想这样做。

If you write code the may you write code (and why not, we all look forward to C++11) then you for now need to add the flag. You can do that in a make file, or you can do something like this in a two-liner /usr/local/bin/g++ :

#!/bin/bash
/usr/bin/g++ -std=c++11 "$@"

which, by being earlier in the path, will "win" over the default g++ binary and call it for you.

Convenient, but your code may now be non-portable. A tradeoff.

Specify -std=c+11 in your build system or IDE of choice. You can learn about various build systems here .

This is specific of GCC, you have to tell it a lot explicitly. If you are using some IDE, just go to project options and specify -std=c++11 there. Othrwise better write C++03 conforming code.

Doing an alias is indeed a safe way to go. I added the following to my .bashrc file for this exact same reason, and it's working flawlessly.

alias g++='g++ -std=c++11'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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