Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which C++ standard is the default when compiling with g++?

Tags:

c++

g++

mingw

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?

like image 764
Manuel Avatar asked Jun 24 '17 08:06

Manuel


People also ask

What does G do in compiling?

The -g option instructs the compiler to generate debugging information during compilation. In C++, the -g option turns on debugging and turns off inlining of functions. The- g0 (zero) option turns on debugging and does not affect inlining of functions.

What is in G ++ compiling?

G++ is a compiler, not merely a preprocessor. G++ builds object code directly from your C++ program source. There is no intermediate C version of the program. (By contrast, for example, some other implementations use a program that generates a C program from your C++ source.)


2 Answers

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:

  • Details on the g++ options used
  • Why this only works for g++ 4.7 or later
like image 163
Michael Burr Avatar answered Sep 22 '22 09:09

Michael Burr


You can also check with gdb

  1. $ g++ example.cpp -g Compile program with -g flag to generate debug info
  2. $ gdb a.out Debug program with gdb
  3. (gdb) b main Put a breakpoint at main
  4. (gdb) run Run program (will pause at breakpoint)
  5. (gdb) info source

Prints out something like:

Current source file is example.cpp Compilation directory is /home/xxx/cpp Located in /home/xxx/cpp/example.cpp Contains 7 lines. Source language is c++. Producer is GNU C++14 6.3.0 20170516 -mtune=generic -march=x86-64 -g. Compiled with DWARF 2 debugging format. Does not include preprocessor macro info. 

There is the standard used by compiler: Producer is GNU C++14

If you recompile your program using -std=c++11 (for example), gdb detects it: Producer is GNU C++11

like image 38
Burrito Avatar answered Sep 22 '22 09:09

Burrito