g++

GNU C++ compiler.

Format

   g++ [options] file-list

Summary

g++ does preprocessing, compilation, assembly, and linking. This process can be stopped at an intermediate stage.

Arguments

The file-list contains the list of source files and object files that are used as input by g++.

Options

-std=c++14 use C++14 standard This option causes g++ to use the C++14 standard. By default, it will use the C++98 standard.
-g include debugger info This option causes g++ to produce debugging information for use by the debugger. This allows you to use gdb to debug your program, but greatly increases the size of the executable file.
-o output-file specify output file name This option tells g++ to place its output in the file output-file. If -o is not specified, the default is to put the executable output in a file named in a.out and the object file for source.cpp or source.cc in source.o.
-Wall show all warnings This option turns on all optional warnings which are desirable for normal code.
-Werror treat warnings as errors This option prevents code with warnings from successfully compiling.
-ansi conform to ISO standard C++ This option turns off g++ features that are incompatible with the ISO C++ standard.
-pedantic conform to strict ISO C++ This option issues all warnings demanded by strict ISO C++ and rejects programs that use forbidden extensions.
-c compile only This option causes g++ to not run the link phase of the build process. No executable file is created and the output consists solely of object files.

Examples

The first command line compiles, assembles, and links a C++ source file in the working directory named prog1.cpp, producing an executable file named a.out. The executable file is then run as a program, producing the output "Hello world".

z123456@turing:~$ g++ -Wall -Werror -std=c++11 prog1.cpp
z123456@turing:~$ ./a.out
Hello world
z123456@turing:~$

The next command line compiles and assembles a C++ source file named prog2.cpp, but does not link it. The output from this command is an object file named prog2.o.

z123456@turing:~$ g++ -Wall -Werror -std=c++11 -c prog2.cpp

The final example compiles and assembles three C++ source files named prog3.cpp, Name.cpp, and Employee.cpp. The object files produced are then linked together along with the object file mylib.o to produce a single executable file named prog3.

z123456@turing:~$ g++ -Wall -Werror -std=c++11 -o prog3 prog3.cpp Name.cpp Employee.cpp mylib.o