g++ command is a GNU c++ compiler invocation command, which is used for preprocessing, compilation, assembly and linking of source code to generate an executable file. The different “options” of g++ command allow us to stop this process at the intermediate stage.
- Check g++ compiler version information:
g++ --version

- Compile a CPP file to generate executable target file: g++ file_name command is used to compile and create an executable file a.out (default target name).
Example: Given a simple program to print “Hello Geek” on standard output with file name hello.cpp
// hello.cpp file#include <iostream>intmain(){std::cout <<"Hello Geek\n";return0;}chevron_rightfilter_noneg++ hello.cpp

This compiles and links hello.cpp to produce a default target executable file a.out in present working directory. To run this program, type ./a.out where ./ represents present working directory and a.out is the executable target file.
./a.out

- g++ -S file_name is used to only compile the file_name and not assembling or linking. It will generate a file_name.s assembly source file.
Example:
g++ -S hello.cpp

- g++ -c file_name is used to only compile and assemble the file_name and not link the object code to produce executable file. It will generate a file_name.o object code file in present working directory.
Example:
g++ -c hello.cpp

- g++ -o target_name file_name: Compiles and links file_name and generates executable target file with target_name (or a.out by default).
Example:
g++ -o main.exe hello.cpp

- Compile and link multiple files: When -c flag is used, it invokes the compiler stage which translates source code to object code.When -o flag is used it links object code to create the executable file from file_name.o to a.out(default), multiples files may be passed together as arguments.
Example:
// hello.cpp file#include "helloWorld.h"#include <iostream>intmain(){std::cout <<"Hello Geek\n";helloWorld();return0;}chevron_rightfilter_none// helloWorld.cpp file#include <iostream>voidhelloWorld(){std::cout <<"Hello World\n";}chevron_rightfilter_none// helloWorld.h filevoidhelloWorld();chevron_rightfilter_noneg++ -c helloWorld.cpp hello.cpp
It compiles and creates object code for the files helloWorld.cpp and hello.cpp to helloWorld.o and hello.o respectively.
g++ -o main.exe helloWorld.o hello.o
It links the object codes helloWorld.o and hello.o to create an executable file main.exe
./main.exe
It runs the executable file main.exe

- g++ -Wall file_name: It prints all warning messages that are generated during compilation of file_name.
Example:
// hello.cpp file#include <iostream>intmain(){inti;std::cout <<"Hello Geek\n";return0;}chevron_rightfilter_noneg++ -Wall hello.cpp

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- script.aculo.us Installation
- How to change the size of a bootstrap pill badge?
- script.aculo.us Sliders setValue option
- script.aculo.us InPlaceEditor loadTextURL Option
- script.aculo.us InPlaceEditor cancelText Option
- script.aculo.us InPlaceEditor okText Option
- script.aculo.us Sliders Values Option
- Cubes and Cube Roots | Class 8 Maths
- Symmetric and Skew Symmetric Matrices | Class 12 Maths
- Material UI | ReactJS AppBar
- How to make a word count in textarea using JavaScript ?
- How to use SVG with :before or :after pseudo element ?
- Difference between dependencies, devDependencies and peerDependencies
- Fluid width with equally spaced div using CSS
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

