Compiling a C++ program with GCC



To compile a C++ program with GCC, we need to download GCC or install the g++ compiler on our system. Otherwise, you will not be able to compile C++ code using GCC.

What is GCC?

The GCC is a tool chain that compiles code. It stands for GNU Compiler Collection, which was developed by the GNU project. The GCC supports various programming languages such as C, C++, Go, etc.

Specific to the C++ language, the GCC provides the g++ compiler, which is developed or designed to compile the C++ source code into executable programs.

Note: When the C++ program compiles using a g++ compiler, it translates the human-readable C++ code to machine-executable instructions.

Let's first install the GCC compiler, and then we can compile the C++ code using GCC:

Installation of GCC/g++ Compiler

To install the GCC/g++ compiler in your Windows operating system, use the following steps:

Step 1: Download MinGW using the following official links:

https://sourceforge.net/projects/mingw/files/Installer/mingw-get-setup.exe/download

Once you open the above link in your browser, the download will start automatically, and you will be able to see a page as follows:

GCC

Step 2: Double-click on the downloaded "mingw-get-setup.exe" file to begin the installation.

Step 3: Click on the Install button:

GCC

Step 3: Change the directory if you want, or else click on the Continue button to proceed:

GCC

Step 4: Click on the Continue button again to proceed next step of installation:

GCC

Step 5: Once the above process is completed right-click on the following packages and "Mark for Installation":

  • mingw32-base
  • mingw32-gcc-g++
  • mingw32-gcc-objc
  • mingw-base

GCC

Step 6: Click on the "Installation" (top-left corner) and select "Apply Changes":

GCC

Once you select "Apply Changes" it will ask you to "Apply", just click on that the installation will be completed. Now, let's move to the final step of installation.

Setting the Environment variable

Step 1: Open the System Environment Variables setting (or press window + s) type environment variable and open it:

Environment Variable

Step 2: Click on Environment Variables and Select the path in "System Variables":

Environment Variable

Step 3: Click on "New" and add the following path (you might be able to see the path in your C drive):

Environment Variable

Step 4: Select Ok -> Ok -> Ok and close the window.

Step 5: Now, verify the successful installation by running the following commands:

  • gcc -version
  • g++ --version
C:\Users\Tutorialspoint>gcc --version
gcc (MinGW.org GCC-6.3.0-1) 6.3.0
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

C:\Users\Tutorialspoint>g++ --version
g++ (MinGW.org GCC-6.3.0-1) 6.3.0
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Compiling the C++ program Using GCC

The following are the steps that will guide you, how to compile the C++ program using the GCC g++ compiler:

Step 1: Create a file with .cpp extension e.g: test.cpp:

Step 2: Open the file in any editor and paste the following C++ code:

#include<iostream>
using namespace std;
int main() {
    cout << "Hello, World!" << endl;
    return 0;
}

Step 3: Open a common prompt and navigate to the folder where your .cpp file is located, e.g, the file is located in Desktop -> cPlus folder:

C:\Users\Tutorialspoint>cd Desktop/cPlus

C:\Users\Tutorialspoint\Desktop\cPlus>

Step 4: Run the following command to create the test.exe file:

C:\Users\Tutorialspoint\Desktop\cPlus>c++ test.cpp -o test.exe

Here are the important points that you should know:

  • g++: It invokes the C++ compiler.
  • test.cpp: It is your source file.
  • -o test.exe: It sets the name of the output executable to test.

Step 5: Now run the following command to see the output:

C:\Users\Tutorialspoint\Desktop\cPlus>test.exe
Hello, World!

The above program produces the following output:

Hello, World!

Conclusion

The GCC (GNU Compiler Collection) provides a g++ compiler specific to C++ language to compile its source code into executable programs. You can run the above commands to compile and run the C++ code to see the output on the command prompt.

Updated on: 2025-08-28T15:43:45+05:30

31K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements