How to Compile and Run a C++ Program using a Command-Line Interface in Windows


In this article, we show how to compile and run a C++ program using a command-line interface in Windows. Usually Command Prompt software would be used for Windows.

So there are many ways to run a C++ program.

One way that you can do is through an IDE such as CodeLite. This has a text editor combined with a console for compiling. It's an all-in-one package.

Another way a C++ program can be run is through using a simple text editor such as notepad, writing a C++ program, and then compile it using a command-line interface such as command prompt. We can then run the compiled file to get the output of the program.

So in order to do this, we first create a C++ program using a text editor such as notepad.

First, create a folder that simulates the workspace folder (which is a folder which contains many different projects). Then create folder within this folder (which simulates a project). Within this project folder, you want to place your C++ file that contains the C++ program that you want to run.

In order to do this, you have to make sure that the file extension is .cpp. This represents a C++ file.

The program that we will create is a basic one shown below.

We save this program as main.cpp. Make sure that the file is a C++ file and not a

So when this program is executed, the statement, "This C++ program works" will appear as its output.

So now that we have our folders and we have our C++ program within our project folder, we can now go the command-line interface, in this case, Command Prompt, in Windows, and run our code.

So open the Command Prompt and navigate to where your folder where your file is located.

Then in the Command Prompt, enter in the following line.

The statement above executes the main.cpp file, so that the program is run.

A breakdown of this statement is shown below.

g++ is the compiler.

-Wall is so that all warnings will be shown during compilation.

-std=c++14 is so that the program is run according to c++14 standards.

main.cpp is the file that we're executing.

If you press 'Enter' and there are no errors, then the program was compiled with no problems.

Now, enter in the following statement below.

With the dir statement, you'll get a list of all files in a directory.

Notice that after you compile the C++ file successfully, you'll see a file named "a.exe".

This is the compiled C++ file that was created during the compiling process.

To then run this exe file, then simply type in the following.

This will run the "a.exe" file, so that its output will appear as in the command-line interface.

You should now see the output of the program.

This is shown below.

Executing a C++ program in the command-prompt in windows

So "a.exe" is the default executable file that is created when you run a program.

It is not really a good idea to have a compiled file be the default of "a.exe". This is because if you have more than one file in your project folder, each "a.exe" will be overridden. Therefore, when you compile a program, you never really want to leave it at the default exe file name of "a.exe". You want to name, preferably whatever is the name of the file.

So, in our example, instead of leaving the file as "a.exe", we are going to name our executable file, "main.exe".

To do this, we use the following line below.

Now the executable file is "main.exe".

This is all shown below.

Executing a C++ program with changing the exe file name in Command Prompt in Windows

Now you don't have to recompile the "main.cpp" program each time that you want to run it.

And this is how to compile run a C++ program using a command-line interface, such as Command Prompt, in Windows.

Related Resources

C++ Hello World Program

HTML Comment Box is loading comments...