使用g ++编译时,哪个C ++标准是默认的? [英] Which C++ standard is the default when compiling with g++?
问题描述
我有一段如下所示的代码。假设它位于一个名为 example.cpp
$ b
#include< ; fstream的>
#include< string> //为了清晰起见编辑后添加的行
$ b $ int main(){
std :: string filename =input.txt; (文件名)中的
std :: ifstream;
返回0;
$ / code>
在windows上,如果我输入 cmd
命令 g ++ example.cpp
,它会失败。我认为这主要是由于链接器抱怨不能从 string
转换为 const char * $ c $但是如果我使用如下附加参数运行编译器:
g ++ -std = c ++ 17 example.cpp code>,它会编译并正常工作,没有问题。
当我运行前一个命令时会发生什么?我猜测C ++编译器的默认版本标准被调用,但我不知道哪个标准?作为程序员/开发人员,我是否总是使用带有额外参数的后者命令?
如果您的 g ++
晚于4.7我认为你可以找到像这样支持的C ++标准的默认版本:
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
一些参考文献:
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;
}
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*
.
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.
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?
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
An example from my machine:
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
Some references:
这篇关于使用g ++编译时,哪个C ++标准是默认的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!