How to Compile and Run a C++ program in CMD

 

Run C++ code in CMD Commands
How to compile and run a C++ program in CMD with MinGW Compiler

In order to run C++ programs in Command prompt, you must first verify whether the MinGW C++ compiler is installed in your system. 

To verify, all you need to do is open the command prompt and type the command -

  g++ --version

If it shows output similar to the one below, then the MinGW C++ compiler is installed in your system. 

Verify MinGW Installation
Verify Installation

Once you've verified the compiler, you can successfully run your C++ code by following the steps below -

  1. Open the folder which contains the C++ code to be compiled and run.
  2. Click on the address bar which contains the path to the present folder, clear its contents and type 'cmd'. This action opens the command prompt in the present folder.

  3. Alternatively, you can also set the path manually in the command prompt by opening it directly via the Start button.
  4. To compile the C++ code, use the command - 
    g++ program_name.cpp -o exe_filename.exe   | where program_name refers to the name of the C++ program/source code you wish to run, exe_filename refers to the name of the executable file that is the result of the C++ source code compilation. You can set any name for exe_filename.
  5. After compiling the source code, if you get no errors, then you can run the code. For running the code, just type the exe_filename in the command prompt. It will prompt the CMD to run the code.

Example -
Running C++ in Command prompt
Running C++ in CMD




Thanks for reading. If you found any errors or have suggestions to improve the article, please mention them in the comments.

Post a Comment

0 Comments