在G ++中更改默认的C ++标准 [英] Change default C++ standard in g++

查看:129
本文介绍了在G ++中更改默认的C ++标准的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了以C ++ 11标准编译程序,我们需要执行以下操作:

 g++ -std=c++11 myProgram.cpp -o myProgramExec

但是我是否可以将g ++的默认标准设置为C ++ 11,这样我就不必一次又一次提及此选项,尽管我也可以在.bashrc中为此添加一个别名:

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

但是我想知道是否有比这更好的方法了.是否有任何可以编辑的g ++配置文件来实现这一目标?还是有一些更简单的方法可以做到这一点?

解决方案

经过一些研究(您可能已经做过了),我还没有找到改变g++ other的默认行为的方法.而不是重建自定义版本或为其添加别名.


为什么这可能是一件好事:

重要的是,每个g++版本均具有一个定义明确的默认行为.请考虑以下问题:如果更改g++的默认行为,并尝试编译其作者无法识别您的配置的C ++项目,则该项目可能无法编译或编译时出现细微错误/意外行为.

在您自己的项目中,您可以轻松地将所有相关标志和选项添加到您的MakefileCMakeLists.txt中,这样就无需再次键入它们.这也将确保其他编译您的项目的人将获得正确的选项,而无论他们的配置如何.

In order to compile a program in C++11 standard we need to do :

 g++ -std=c++11 myProgram.cpp -o myProgramExec

But is it possible that i can set the default standard of g++ as C++11, so that i don't have to mention this option again and again Although i can also add an alias for this in my .bashrc :

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

But i wonder if there is any better way than this.Is there any config file of g++ which can be edited in order to achieve this? Or is there some easier way to do this?

解决方案

After a bit of research (which you've probably already done yourself), I haven't found a way to change the default behavior of g++ other than rebuilding a custom version or aliasing it.


Why this is probably a good thing:

It is important that each version of g++ has a single, well-defined default behavior. Consider this: if you change the default behavior of g++ and try to compile a C++ project whose author could not be aware of your configuration, the project may fail to compile or compile with subtle errors / unexpected behaviors.

In your own project, you can easily add all the relevant flags and options to your Makefile or CMakeLists.txt so that you don't need to type them again. This will also ensure that other people compiling your project will receive the correct options regardless of their configuration.

这篇关于在G ++中更改默认的C ++标准的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆