First Windows program. G++ compiler

Installing the compiler

Let’s look at creating our first simple C++ program using the g++ compiler, which is currently one of the most popular C++ compilers available for various platforms and distributed as part of the GCC compiler package. More information about g++ can be found on the project’s official website at https://gcc.gnu.org/.

The GCC compiler suite is distributed in various versions. For Windows, one of the most popular versions is the MSYS2 development kit from the non-profit project. It should be noted that MSYS2 requires a 64-bit version of Windows 7 or higher (meaning Vista, XP, and earlier versions are not compatible).

So, let’s download the MSYS2 installer from the official MSYS2 website :

11

 

After downloading, run the installation program:

Generated_Image

 

The first step of the installation will prompt you to set the installation directory. By default, this is C:\msys64:

Generated_Image_7g7maf7g7maf7g7m

 

We’ll leave the default installation directory (you can change it if desired). The next step involves setting up the Start menu shortcut, and then the actual installation will occur. Once the installation is complete, a final window will appear, in which we click the Finish button.

767

 

After installation is complete, the MSYS2.exe console application will launch. If for some reason it doesn’t launch, find the usrt_64.exe file in the C:/msys64 installation folder :

tryr

 

Now we need to install the GCC compiler suite itself. To do this, enter the following command in this application:

pacman -S mingw-w64-ucrt-x86_64-gcc

MSYS2 uses the Packman package manager to manage packages. This command tells packman to install the package mingw-w64-ucrt-x86_64-gcc, which represents the GCC compiler suite (the name of the package to be installed is specified after the parameter. -S).

645

 

Once the installation is complete, we can begin programming in C++. If we open the installation directory and navigate to the C:\msys64\ucrt64\bin folder, we’ll find all the necessary compiler files there:

 

55

 

In particular, the g++.exe file will represent the compiler for the C++ language.

Next, to simplify launching the compiler, we can add its path to the Environment Variables. To do this, enter “edit environment variables for the current user” in the Windows search box:

3423

 

The Environment Variables window will open:

 

57

 

And add the path to the compiler. C:\msys64\ucrt64\bin:

 

9ui9uhi

 

 

To verify that the GCC compiler suite has been installed successfully, enter the following command:

gcc --version

In this case, we should see the version of the compilers

 

12

 

Creating the first program

So, the compiler is installed, and now we can write our first program. To do this, you’ll need a text editor to type the source code. You can use the popular Visual Studio Code editor or even the built-in Notepad.

So, let’s create a folder for source files on the C hard drive. In this folder, create a new text file and rename it hello.cpp. Essentially, C++ source code files are simply text files that typically have the cpp extension.

43

 

In my case,hello.cpp file is located in the C:\cpp folder.

Now let’s define the simplest code in the hello.cpp file that will output a string to the console:

 

1
2
3
4
5
6
7
#include <iostream>               //  header file iostream<font></font>
<font></font>
int main()                          // define a function main<font></font>
{                                   // start of function<font></font>
    std::cout << "Hello 365education.org!";     //we output a line to the console<font></font>
    return 0;                       // exit the function<font></font>
}                                   // end of function<font></font>

 

 

To output a string to the console, you need to enable the required functionality. To do this, add the following line at the beginning of the file:

1
#include <iostream>

This line represents a preprocessor directive that enables the iostream library. This library is needed to output a string to the console.

Next comes the definition of the main function. The main function must be present in any C++ program; it is where the application execution actually begins.

The main function consists of four elements:

  • Return type. In this case, it is int. This type indicates that the function should return an integer.
  • Function name. In this case, the function is called main.
  • Parameter list. After the function name, there is a list of parameters in parentheses. However, in this case, the parentheses are empty, meaning the main function takes no parameters.

Function body. After the parameter list, the function body appears in curly braces. This is where the actual actions performed by the main function are defined.

 

1
2
3
4
{<font></font>
    std::cout << "Hello 365education.org!";<font></font>
    return 0;<font></font>
}

 

The function body prints a string to the console. The standard output stream, std::cout, is used to access the console. Using the << operator, the string of characters to be printed is passed to this stream (in this case, the console itself), i.e., “Hello 365education.org!”

Finally, we exit the function using the return statement. Since the function must return an integer, the value 0 is specified after the return statement. Zero is used as an indicator of successful program completion.

After each statement in the C++ language, a semicolon is placed.

A comment accompanies each line. Anything written after the double slash // represents a comment. Comments are not compiled into the application and are not part of the program code; they serve only to describe it. Comments help you understand what the program does.

08o

 

Now let’s compile this file. To do this, open the Windows command prompt and first use the cd command to navigate to the folder containing the source file:

cd C:\cpp

To compile the source code, you need to pass the hello.cpp file as a parameter to the gcc compiler:

g++ hello.cpp -o hello

This optional parameter -o hellospecifies that the compiled file will be named hello.exe. If this parameter is omitted, the file will be named a.exe by default.

After running this command, an executable file will be compiled, which in Windows is named hello.exe by default. We can access this file, and in this case, the console will print the string “Hello 365education.org!”, exactly as specified in the code.

56u67

 

If you use the PowerShell shell instead of the command line, you need to write “./hello” to run the file.

It’s worth noting that we can combine compilation and execution with the following command:

 

g++ hello.cpp -o hello.exe & hello.exe

Next

Explore More IT Terms

RELATED ARTICLES

Leave a Reply

Your email address will not be published. Required fields are marked *