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?
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.
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.)
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++
options usedg++
4.7 or laterYou can also check with gdb
$ g++ example.cpp -g
Compile program with -g flag to generate debug info$ gdb a.out
Debug program with gdb(gdb) b main
Put a breakpoint at main(gdb) run
Run program (will pause at breakpoint)(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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With