使用 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
一些参考资料:
- 使用的
g++
选项的详细信息 - 为什么这只适用于
g++
4.7 或更高版本
- Details on the
g++
options used - Why this only works for
g++
4.7 or later
相关文章