![]() |
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 Installation |
Once you've verified the compiler, you can successfully run your C++ code by following the steps below -
- Open the folder which contains the C++ code to be compiled and run.
- 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.
- Alternatively, you can also set the path manually in the command prompt by opening it directly via the Start button.
- 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. - 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 -
Thanks for reading. If you found any errors or have suggestions to improve the article, please mention them in the comments.
0 Comments