How to run c++ code locally
Objective:
- To setup your development environment.
- To compile and run an cpp parallel program.
Software:
- MinGW Installation Manager or MinGW-w64
- Visual Studio code
Installing g++ compiler
In this tutorial, I explain two ways to install g++ compiler:
-
MinGW (Minimalist GUN for Windows, originally called mingw32) is a native Windows port of the GNU Compiler Collection (GCC) that provides header files and free distributable import libraries for creating native Windows applications.
Note that all the programs produced under MinGW are 32-bit executables, but they can be used in 32-bit and 64-bit versions of Windows. -
MinGW-w64 is an advancement of the original MinGW project. It was forked in 2007 in order to provide support for 64 bits and new APIs.
Note that MinGW-w64 supports binaries native to Windows 64-bit and 32-bit operating systems.
Installing g++ compiler use MinGW Installation Manager
- Visit the website MinGW and click on Download.
- Once the file is downloaded. Open the
mingw-get-setup.exefile then click on [Install]
- I am going to install MinGW under
C:directory, click on [continue]
- Wait MinGW to install then click on [continue]
- In
MinGW Installation Managerselectmingw32-base-binby clicking on the square control beside the package name. You should see as below:
- Also, you should select all the following package as below:
- On the menu bar, select
InstallationApply Changesas shown below:
- Click on the
Applyas shown below:
- Finally, After the installation finish click on
Close - After that, go to the installation directory [in my case
C:\MinGW\bin] and copy the directory of the bin folder as shown below:
- Go to the setting and write
envon the search bar, selectEdit environment variables for your accountas shown below:
- Select
pathand click onEditas shown below:
- Click on
Newto add your copied MinGW path [ in my caseC:\MinGW\bin]
- Finally click on OK OK
Installing g++ compiler use MinGW-w64
MinGW-w64 comes in three flavors for Windows: GitHub, WinLibs, or MSYS2. Here we will use the WinLibs flavor (a standalone build of GCC and MinGW-w64 for Windows).
- Visit the website WinLibs. Here we will download release versions of UCRT (Univarsal C Runtime) with the threading library POSIX threads/pthread.h. Under UCRT, select the GCC version with POSIX threads. For example, click on Win32 (without LLVM/Clang/LLD/LLDB) or Win64 (without LLVM/Clang/LLD/LLDB) based on your system as shown below, or download GCC 14.2.0 (with POSIX threads) directly from here: Win32 or Win64

- Extract the Winlibs downloaded file to the
C:directory, as shown below.
- Copy the directory of the bin folder
C:\mingw64\binin my case, then add it to the environment variable path, following the same steps from 11 to 14 on Installing g++ compiler, use MinGW Installation Manager
Installing Visual Studio Code
- Visit the website VS code Click on
Windowsto download VS code for Windows, as shown below
- After the download finished open the
VSCodeUserSetupexecutable file, when it open selectI accept the agreementthen click onNext
-
Select all the option as seen below
-
Finally we are ready to install the VS code, click on
installand wait untail the setup finish - On the Vs code, on the left-hand side, click on
extensionthen in the search bar, writeC++selectC/C++and click onInstallas shown below
- Close
Vs code
Running C++ parallel code on VS code
- Right click on the Windows button
or press windows + xthen selectWindows PowerShell Adminas shown below
- Navigate to
D:directory by writecd d:as shown below
- Then make directory named
Parallel-codes[mkdir Parallel-codes] as shown below
- Then navigate to the directory
- Write
code .to open VS code on thed:\Parallel-codesdirectory
- After
VS codeopen, create new file name itfibo.cppand write Fibonacci code in the end of Lab(1) savefibo.cpp,then on the top bar select on…Terminal New Terminal. As shown below
- Before to add
pthread.hheader file you need to installpthreadslibrary also known asPOSIXthreads, which is provides a way to create and manage threads in multi-threaded program. To install the library usemingw-getcommand and writemingw-get install pthreadson the terminal
Skip this step if you are installing g++ compiler use MinGW-w64
- Compile the
fibo.cppprogram. Write this command:g++ -o fibo -pthread fibo.cppon the terminal.
Let us explain the command in detail:
g++: GUN Compiler Collection for c++
-o fibo:-oflag to create the output file namedfibo
-pthreadflag: tell the compiler to link with thepthreadlibrary
fibo.cpp: the name of the program
Note that: if you want to useopenMPon your program [omp.hheader file]. You need to add [-fopenmp] flag [g++ -o out –fopenmp program.cpp] to compile the program.
- Finally write
./fiboon the terminal to runfiboprogram