Run C++ in command prompt - Windows

Learn run c++ in command prompt - windows with practical examples, diagrams, and best practices. Covers c++, windows, command-line development techniques with visual explanations.

Compile and Run C++ Programs from the Windows Command Prompt

Hero image for Run C++ in command prompt - Windows

Learn how to set up your Windows environment, compile C++ code using MinGW's GCC, and execute your programs directly from the command line.

Running C++ programs from the command prompt on Windows is a fundamental skill for developers. It allows for greater control over the compilation process, script automation, and a deeper understanding of how your code is built and executed. This guide will walk you through setting up your environment, compiling your C++ code, and running it using the MinGW (Minimalist GNU for Windows) toolchain.

1. Setting Up Your C++ Development Environment (MinGW)

Before you can compile and run C++ code, you need a C++ compiler. MinGW provides a complete open-source software development environment for Windows, including the GCC (GNU Compiler Collection) compiler. Follow these steps to install MinGW and configure your system's PATH environment variable.

1. Download MinGW-w64

Go to the official MinGW-w64 website or a trusted mirror (e.g., SourceForge) and download the installer. Look for a build that includes posix threads and seh or sjlj exceptions, depending on your system architecture (32-bit or 64-bit).

2. Install MinGW-w64

Run the installer. It's recommended to install it to a simple path like C:\MinGW or C:\MinGW-w64 to avoid issues with spaces in directory names. Ensure that the bin directory containing g++.exe is included in the installation.

3. Add MinGW to System PATH

This is crucial for running g++ commands from any directory in your command prompt.

  1. Search for "Environment Variables" in the Windows search bar and select "Edit the system environment variables."
  2. Click "Environment Variables..." in the System Properties window.
  3. Under "System variables," find and select the Path variable, then click "Edit."
  4. Click "New" and add the path to your MinGW bin directory (e.g., C:\MinGW\bin or C:\MinGW-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin).
  5. Click "OK" on all windows to save the changes.

4. Verify Installation

Open a new Command Prompt (CMD) window and type g++ --version. If the installation was successful, you should see the GCC version information. If not, double-check your PATH variable and restart your command prompt.

2. Writing Your First C++ Program

Let's create a simple "Hello, World!" program to test our setup. You can use any text editor like Notepad, VS Code, or Sublime Text to write your C++ code.

#include <iostream>

int main() {
    std::cout << "Hello, Command Prompt!" << std::endl;
    return 0;
}

Save this file as hello.cpp in a directory of your choice, for example, C:\Users\YourUser\Documents\cpp_projects.

3. Compiling Your C++ Program

Now that you have your C++ code and the compiler set up, you can compile your source file into an executable. Open your command prompt and navigate to the directory where you saved hello.cpp.

cd C:\Users\YourUser\Documents\cpp_projects
g++ hello.cpp -o hello.exe

Navigate to your project directory and compile the C++ file.

Let's break down the compilation command:

  • g++: This invokes the GNU C++ compiler.
  • hello.cpp: This is your source code file.
  • -o hello.exe: This is the output flag, specifying the name of the executable file to be created. If you omit -o, the default executable name will be a.exe on Windows.

4. Running Your C++ Executable

Once the compilation is successful, an executable file (hello.exe in this case) will be created in the same directory. You can now run it directly from the command prompt.

.\hello.exe

Execute the compiled C++ program.

You should see the output: Hello, Command Prompt! displayed in your command prompt window.

flowchart TD
    A[Start] --> B{Install MinGW-w64};
    B --> C[Add MinGW bin to PATH];
    C --> D{Verify g++ installation};
    D --> E[Write C++ Source Code (e.g., hello.cpp)];
    E --> F[Open Command Prompt];
    F --> G[Navigate to Source Directory];
    G --> H[Compile: g++ hello.cpp -o hello.exe];
    H --> I{Compilation Successful?};
    I -- Yes --> J[Run: .\hello.exe];
    I -- No --> K[Debug Code/Compiler Setup];
    J --> L[End];
    K --> E;

Workflow for compiling and running C++ from the command prompt.

5. Advanced Compilation Options

The g++ compiler offers many options for more complex projects. Here are a few common ones:

Enable C++11/14/17/20 Features

To use modern C++ features, you need to specify the C++ standard:

g++ -std=c++17 hello.cpp -o hello.exe

Include Directories

If your project uses custom header files located in a different directory, use the -I flag:

g++ -I C:\MyProject\include main.cpp -o main.exe

For projects that use external libraries (e.g., math library), you need to link them using the -l flag and specify the library path with -L:

g++ main.cpp -o main.exe -L C:\MyProject\lib -lmycustomlib

Optimization Flags

Optimize your code for performance or size using flags like -O2 or -Os:

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

Debugging Information

Include debugging symbols with -g to use a debugger:

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