使用 g++ 编译时,哪个 C++ 标准是默认的? [英] Which C++ standard is the default when compiling with g++?

查看:57
本文介绍了使用 g++ 编译时,哪个 C++ 标准是默认的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段代码如下所示.假设它在一个名为 example.cpp

I have a piece of code that looks like the following. Let's say it's in a file named example.cpp

#include <fstream>
#include <string> // line added after edit for clarity

int main() {
    std::string filename = "input.txt";
    std::ifstream in(filename);

    return 0;
}

在 Windows 上,如果我在 cmd 中键入命令 g++ example.cpp,它将失败.这是一长串错误,我认为主要是由于链接器抱怨无法从 string 转换为 const char*.

On a windows, if I type in the cmd the command g++ example.cpp, it will fail. It's a long list of errors I think mostly due to the linker complaining about not being able to convert from string to const char*.

但如果我使用像这样的附加参数运行编译器:g++ -std=c++17 example.cpp,它将编译并正常工作,没有任何问题.

But if I run the compiler using an additional argument like so: g++ -std=c++17 example.cpp, it will compile and work fine with no problems.

当我运行前一个命令时会发生什么?我猜 C++ 编译器的默认 version 标准会被调用,但我不知道是哪个?作为程序员/开发人员,我是否应该始终使用带有额外参数的后一个命令?

What happens when I run the former command? I'm guessing a default version standard of the C++ compiler gets called, but I don't know which? And as a programmer/developer, should I always use the latter command with the extra argument?

推荐答案

如果你的 g++ 版本晚于 4.7 我想你可以找到支持的 C++ 标准的默认版本,如下所示:

If your version of g++ is later than 4.7 I think you can find the default version of C++ standard supported like so:

g++ -dM -E -x c++  /dev/null | grep -F __cplusplus

我机器上的一个例子:

mburr@mint17 ~ $ g++ --version | head -1
g++ (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
mburr@mint17 ~ $ g++ -dM -E -x c++  /dev/null | grep -F __cplusplus
#define __cplusplus 199711L

一些参考资料:

  • Details on the g++ options used
  • Why this only works for g++ 4.7 or later

这篇关于使用 g++ 编译时,哪个 C++ 标准是默认的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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