Compiling and Running C++ Code from the Command Line
Many developers, especially those starting out, prefer the simplicity of writing code in a text editor like Notepad++ and compiling/running it directly from the command line. This tutorial will guide you through the process of setting up your environment and compiling/running C++ code without relying on an Integrated Development Environment (IDE).
Prerequisites
- A Text Editor: Notepad++, Sublime Text, VS Code (with C++ extension) or any plain text editor will do.
- A C++ Compiler: This is the key component. Several options are available:
- GCC (GNU Compiler Collection): A widely used, powerful, and free compiler. For Windows, you’ll typically use a distribution like MinGW (Minimalist GNU for Windows) or Cygwin to provide a GCC environment.
- Clang: Another excellent, modern compiler, often used as an alternative to GCC. Similar to GCC, you’ll usually need a distribution for Windows.
- MSVC (Microsoft Visual C++): The compiler included with Microsoft Visual Studio. If you have Visual Studio installed, MSVC is already available. You can install the Build Tools for Visual Studio to get MSVC without the full IDE.
Installation and Setup
The installation process varies depending on your chosen compiler:
1. Installing MinGW (for GCC on Windows):
- Download the MinGW installer from a source like SourceForge: https://sourceforge.net/projects/mingw/
- Run the installer and select the
gcc
andg++
packages (and any other libraries you anticipate needing). - After installation, crucially, you need to add the MinGW
bin
directory to your system’sPATH
environment variable. This allows you to execute thegcc
andg++
commands from any directory in the command prompt.- To do this, search for "Environment Variables" in the Windows search bar.
- Click "Edit the system environment variables".
- Click "Environment Variables…".
- Under "System variables", find the "Path" variable and select "Edit…".
- Click "New" and add the path to the
bin
directory within your MinGW installation (e.g.,C:\MinGW\bin
). - Restart your command prompt after making these changes.
2. Installing MSVC (Microsoft Visual C++):
- Download and install the Build Tools for Visual Studio from Microsoft’s website: https://visualstudio.microsoft.com/downloads/#build-tools-for-visual-studio-2022
- During installation, make sure to select the "C++ build tools" workload.
- After installation, open the "Developer Command Prompt for VS 2022" (or the appropriate version you installed) to access the MSVC compiler. This command prompt automatically sets up the necessary environment variables.
Compiling Your Code
Once your compiler is installed and configured, you can compile your C++ code.
-
Save your code: Create a C++ source file (e.g.,
hello.cpp
) and save it with a.cpp
extension.#include <iostream> int main() { std::cout << "Hello, world!" << std::endl; return 0; }
-
Open the command prompt: Navigate to the directory where you saved your
.cpp
file using thecd
command. For example:cd C:\Users\YourName\Documents\cpp_projects
-
Compile the code: Use the compiler command to compile your source file. The specific command varies depending on your compiler:
-
GCC/MinGW:
g++ hello.cpp -o hello.exe
-
MSVC (using the Developer Command Prompt):
cl /EHsc hello.cpp
-
Explanation of compiler flags:
g++
orcl
: The C++ compiler command.hello.cpp
: The source file to compile.-o hello.exe
(GCC): Specifies the output executable file name (hello.exe
)./EHsc
(MSVC): Enables exception handling and stack unwinding.
-
Running Your Code
After successful compilation, an executable file (e.g., hello.exe
) will be created in the same directory.
-
Run the executable: Type the name of the executable file in the command prompt and press Enter:
hello.exe
This will execute your program, and you should see the output printed to the command prompt.
Batch Script for Convenience (Windows)
To simplify the compilation and execution process, you can create a batch script. Create a file named cppExecutor.bat
(or any other name you prefer) with the following content:
@echo off & cls
set /p pathName=Enter The Path where the file is located:%=%
cd %pathName%
set /p file=Enter The Name of the file you want to compile:%=%
g++ -o %file% %file%.cpp
%file%.exe
Save this file in a convenient location. Now, you can run this batch script, and it will prompt you for the directory and the filename. It will then compile and execute your code automatically.
Conclusion
This tutorial has demonstrated how to compile and run C++ code from the command line. While IDEs offer many features, understanding the compilation process directly is valuable for any C++ developer. Experiment with different compiler flags and options to customize the build process and gain a deeper understanding of C++ development.