Skip to content Skip to sidebar Skip to footer

[ENG VER] Preparation of C/C++ Programming in Visual Studio Code (Using MSYS2 and MinGW Compiler Software) [TUTORIAL]

Assalamu‘alaikum Wr. Wb.

Hello guys! Do you like coding? If you like Coding, would it be difficult to run Programs for C or C++ Programming if you use Visual Studio Code (VS Code) Software? This time I will give a C/C++ Programming Preparation Tutorial in Visual Studio Code using the MSYS2 and MinGW Compiler Software.



Article Source : Teknisiserbabisa.com

Actually for C Programming Language only requires 2 (Two) Tools, namely Compiler and Text Editor (VS Code, Sublime Text, etc.).
 
A text editor is a tool or program that is used to write code from a program. While the compiler is a program used to translate C language into machine language so that it can be understood by computers.
 
There is also a Text Editor that already has a compiler, the program is called the Integrated Development Environment (IDE).


EDITOR TEXT PREPARATIONS

There are lots of text editor options that can be used to write C and C++ programs, for example, Notepad, Notepad++, Gedit, Geany, Mousepad, Kate, Atom, VS Code, Sublime Text, etc.

In accordance with the title of this article, use Visual Studio Code. You can download at the Link below.


If you already have Visual Studio Code (VS Code) installed, then follow the rest of the instructions.


COMPILER

Source (For MSYS2 Compiler Download) : MSYS2.org

A compiler is a program that functions to Translate C Language into Machine Language, so that it can be understood by computers. There are also many types of C compilers, there are GCC (GNU C Compiler), Clang, lcc, LuxCC, Tiny C Compiler, Borland Turbo C, VBCC, XL C, Small-C, etc.
 
On this occasion we try to install MinGW as its C/C++ language compiler.



However, in this post I will do the MSYS2 Software Installation, because this software is very complete and also has MinGW. And here, here's a Tutorial to download (Download) MSYS2.

MSYS2 is a collection of tools and libraries providing you with an easy-to-use environment for building, installing and running native Windows software.

It consists of a command line terminal called mintty, bash, version control systems like git and subversion, tools like tar and awk and even build systems like autotools, all based on a modified version of Cygwin. Despite some of these central parts being based on Cygwin, the main focus of MSYS2 is to provide a build environment for native Windows software and the Cygwin-using parts are kept at a minimum. MSYS2 provides up-to-date native builds for GCC, mingw-w64, CPython, CMake, Meson, OpenSSL, FFmpeg, Rust, Ruby, just to name a few.

To provide easy installation of packages and a way to keep them updated it features a package management system called Pacman, which should be familiar to Arch Linux users. It brings many powerful features such as dependency resolution and simple complete system upgrades, as well as straight-forward and reproducible package building. Our package repository contains more than 2600 pre-built packages ready to install.

For more details see 'What is MSYS2?' which also compares MSYS2 to other software distributions and development environments like Cygwin, WSL, Chocolatey, Scoop, ... and 'Who Is Using MSYS2?' to see which projects are using MSYS2 and what for.

1. Download File

First, you have to open on this Site first. Then download the file here (msys2-x86_64-20210725.exe). Verify with SHA256 checksum 7e055b71306e64192e2612f959f54ae99a5cf57186206ac702d113ef00ba35c0 or GPG Signature with 0xf7a49b0ec. Then Run the installer. MSYS2 requires 64 bit Windows 7 or later.

2. Enter the Installation Folder you want

Enter the Installation Folder you want (jShort path ASCII only on NTFS volume, no accents, no spaces, no symlinks, no substs or network drives, no FAT).


3. Installing the 64bit MSYS Program

When done, tick Run MSYS2 now.


4. Running the MSYS2 Program

Update the package database and base packages. Unless your setup file is very recent, it will take two steps. First run pacman -Syu :

$ pacman -Syu
:: Synchronizing package databases...
 mingw32                        805.0 KiB
 mingw32.sig                    438.0   B
 mingw64                        807.9 KiB
 mingw64.sig                    438.0   B
 msys                           289.3 KiB
 msys.sig                       438.0   B
:: Starting core system upgrade...
warning: terminate other MSYS2 programs before proceeding
resolving dependencies...
looking for conflicting packages...

Packages (6) bash-5.1.004-1  filesystem-2021.01-1
             mintty-1~3.4.4-1  msys2-runtime-3.1.7-4
             pacman-5.2.2-9  pacman-mirrors-20201208-1

Total Download Size:   11.05 MiB
Total Installed Size:  53.92 MiB
Net Upgrade Size:      -1.24 MiB

:: Proceed with installation? [Y/n]
:: Retrieving packages...
 bash-5.1.004-1-x86_64            2.3 MiB
 filesystem-2021.01-1-any        33.2 KiB
 mintty-1~3.4.4-1-x86_64        767.2 KiB
 msys2-runtime-3.1.7-4-x86_64     2.6 MiB
 pacman-mirrors-20201208-1-any    3.8 KiB
 pacman-5.2.2-9-x86_64            5.4 MiB
(6/6) checking keys in keyring       100%
(6/6) checking package integrity     100%
(6/6) loading package files          100%
(6/6) checking for file conflicts    100%
(6/6) checking available disk space  100%
:: Processing package changes...
(1/6) upgrading bash                 100%
(2/6) upgrading filesystem           100%
(3/6) upgrading mintty               100%
(4/6) upgrading msys2-runtime        100%
(5/6) upgrading pacman-mirrors       100%
(6/6) upgrading pacman               100%
:: To complete this update all MSYS2 processes including this terminal will be closed. Confirm to proceed [Y/n]

When finished, click Y (Yes), then follow the next steps. Run "MSYS2 MSYS" from the Start menu. Update the rest of the base packages with pacman -Su :

$ pacman -Su
:: Starting core system upgrade...
 there is nothing to do
:: Starting full system upgrade...
resolving dependencies...
looking for conflicting packages...

Packages (20) base-2020.12-1  bsdtar-3.5.0-1
              [... more packages listed ...]

Total Download Size:   12.82 MiB
Total Installed Size:  44.25 MiB
Net Upgrade Size:       3.01 MiB

:: Proceed with installation? [Y/n]
[... downloading and installation continues ...]

When finished, click Y (Yes), then follow the next steps. Now MSYS2 is ready for you. You may want to install some GCC tools and mingw-w64 to start compiling :

$ pacman -S --needed base-devel mingw-w64-x86_64-toolchain
warning: file-5.39-2 is up to date -- skipping
[... more warnings ...]
:: There are 48 members in group base-devel:
:: Repository msys
   1) asciidoc  2) autoconf  3) autoconf2.13  4) autogen
   [... more packages listed ...]

Enter a selection (default=all):
:: There are 19 members in group mingw-w64-x86_64-toolchain:
:: Repository mingw64
   1) mingw-w64-x86_64-binutils  2) mingw-w64-x86_64-crt-git
   [... more packages listed ...]

Enter a selection (default=all):
resolving dependencies...
looking for conflicting packages...

Packages (123) docbook-xml-4.5-2  docbook-xsl-1.79.2-1
               [... more packages listed ...]
               m4-1.4.18-2  make-4.3-1  man-db-2.9.3-1
               mingw-w64-x86_64-binutils-2.35.1-3
               mingw-w64-x86_64-crt-git-9.0.0.6090.ad98746a-1
               mingw-w64-x86_64-gcc-10.2.0-6
               mingw-w64-x86_64-gcc-ada-10.2.0-6
               mingw-w64-x86_64-gcc-fortran-10.2.0-6
               mingw-w64-x86_64-gcc-libgfortran-10.2.0-6
               mingw-w64-x86_64-gcc-libs-10.2.0-6
               mingw-w64-x86_64-gcc-objc-10.2.0-6
               mingw-w64-x86_64-gdb-10.1-2
               mingw-w64-x86_64-gdb-multiarch-10.1-2
              [... more packages listed ...]

Total Download Size:    196.15 MiB
Total Installed Size:  1254.96 MiB

:: Proceed with installation? [Y/n]
[... downloading and installation continues ...]

When finished, click Y (Yes), then follow the next steps.

5. Follow the next Step

To start building using mingw-w64 GCC, close this window and run "MSYS MinGW 64-bit" from the Start menu. Now you can call make or gcc to build software for Windows.

See the introductory page to learn which Start menu items to use when and which packages to install. See the detailed MSYS2 installation guide for troubleshooting and additional details on how to keep your MSYS2 up to date.


PREPARATION FOR VISUAL STUDIO CODE

Source (Visual Studio Code) : Code.Visualstudio.com

After finished Installing MinGW on MSYS2 on your Device, there is more work to do next. This time specifically for the Visual Studio Code Text Editor. First of all, please open your VS Code application and then open the extension window with Ctrl+Shift+X.

C/C++

This extension serves to provide all language support for C/C++ to Visual Studio Code, including features like IntelliSense and debugging.

Code Runner

By using this Extension can run several programming languages ​​by using some special commands. Programming languages ​​supported include: C, C++, Java, JavaScript, PHP, Python, Perl, Perl 6, Ruby, Go, Lua, Groovy, PowerShell, BAT/CMD, BASH/SH, F# Script, F# (.NET Core) , C# Script, C# (.NET Core), VBScript, TypeScript, CoffeeScript, Scala, Swift, Julia, Crystal, OCaml Script, R, AppleScript, Elixir, Visual Basic .NET, Clojure, Haxe, Objective-C, Objective-C++ , Rust, Racket, Scheme, AutoHotkey, AutoIt, Kotlin, Dart, Free Pascal, Haskell, Nim, D, Lisp, Kit, V, SCSS, Sass, CUDA, Less, Fortran, and several language customizations made.

After configuring VS Code, you will compile and debug a simple Hello World program in VS Code. This tutorial doesn't teach you about GCC, GDB, Mingw-w64, or the C++ language. For these subjects, there are many good resources available on the Web.

If you have any issues, feel free to file an issue for this tutorial in the VS Code documentation repository.

Visual Studio Code Settings for C/C++ Language (Prerequisite)

To successfully complete this Tutorial, you must perform the following steps:

1. Install Visual Studio Code.

2. Install C/C++ Extension for VS Code. You can install C/C++ extensions by searching for 'c++' in Extensions view (Ctrl+Shift+X).


3. Get the latest version of Mingw-w64 via MSYS2, which provides native builds of GCC, Mingw-w64, and other useful C++ Tools and Libraries. Click here to download the MSYS2 installer. Then follow the instructions on the MSYS2 Website to install Mingw-w64.

4. Add the Path to your Mingw-w64 bin folder to the Windows PATH environment variable using the following steps :
  1. In the Windows search bar, type 'settings' to open your Windows Settings.
  2. Search for Edit Environment Variables for your Account.
  3. Choose the Path variable in your User variables and then select Edit.
  4. Select New and add the Mingw-w64 destination folder path to the system path. The exact path depends on which version of Mingw-w64 you have installed and where you installed it. If you used the settings above to install Mingw-w64, then add this to the path C:\msys64\mingw64\bin.
  5. Select OK to save the updated PATH. You will need to reopen any console windows for the new PATH location to be available.

Check Your MinGW Installation

To check that your Mingw-w64 tools are correctly installed and available, open a new Command Prompt and type :

g++ --version
gdb --version

Take a look at the Image below to perform MinGW Version Check via Command Prompt :


If you don't see the expected output or g++ or gdb is not a recognized command, make sure your PATH entry matches the Mingw-w64 binary location where the compilers are located. If the compilers do not exist at that PATH entry, make sure you followed the instructions on the MSYS2 website to install Mingw-w64.

Create "Hello World"

From a Windows command prompt, create an empty folder called projects where you can place all your VS Code projects. Then create a sub-folder called helloworld, navigate into it, and open VS Code in that folder by entering the following commands :

mkdir projects
cd projects
mkdir helloworld
cd helloworld
code .

The "code ." command opens VS Code in the current working folder, which becomes your "workspace". Accept the Workspace Trust dialog by selecting Yes, I trust the authors since this is a folder you created. As you go through the tutorial, you will see three files created in a .vscode folder in the workspace :
  • tasks.json  (Build Instructions)
  • launch.json  (Debugger Settings)
  • c_cpp_properties.json  (Compiler Path and IntelliSense settings)

[For more reading, please see here (Code.Visualstudio.com).]

C/C++ Configurations

If you want more control over the C/C++ extension, you can create a c_cpp_properties.json file, which will allow you to change settings such as the path to the compiler, include paths, C++ standard (default is C++17), and more.

You can view the C/C++ configuration UI by running the command C/C++: Edit Configurations (UI) from the Command Palette (Ctrl+Shift+P).


This opens the C/C++ Configurations page. When you make changes here, VS Code writes them to a file called c_cpp_properties.json in the .vscode folder.

Here, we've changed the Configuration name to GCC, set the Compiler path dropdown to the g++ compiler, and the IntelliSense mode to match the compiler (gcc-x64)


Visual Studio Code places these settings in .vscode\c_cpp_properties.json. If you open that file directly, it should look something like this :

{
  "configurations": [
    {
      "name": "GCC",
      "includePath": ["${workspaceFolder}/**"],
      "defines": ["_DEBUG", "UNICODE", "_UNICODE"],
      "windowsSdkVersion": "10.0.18362.0",
      "compilerPath": "C:/msys64/mingw64/bin/g++.exe",
      "cStandard": "c17",
      "cppStandard": "c++17",
      "intelliSenseMode": "windows-gcc-x64"
    }
  ],
  "version": 4
}

You only need to add to the Include path array setting if your program includes header files that are not in your workspace or in the standard library path.

Compiler Path

The extension uses the compilerPath setting to infer the path to the C++ standard library header files. When the extension knows where to find those files, it can provide features like smart completions and Go to Definition navigation.

The C/C++ extension attempts to populate compilerPath with the default compiler location based on what it finds on your system. The extension looks in several common compiler locations.

The compilerPath search order is :
  • First check for the Microsoft Visual C++ compiler
  • Then look for g++ on Windows Subsystem for Linux (WSL)
  • Then g++ for Mingw-w64.
If you have Visual Studio or WSL installed, you may need to change compilerPath to match the preferred compiler for your project. For example, if you installed Mingw-w64 version 8.1.0 using the i686 architecture, Win32 threading, and sjlj exception handling install options, the path would look like this: C:\Program Files (x86)\mingw-w64\i686-8.1.0-win32-sjlj-rt_v6-rev0\mingw64\bin\g++.exe.

Troubleshooting

You will need to follow the steps on the MSYS2 website and use the MSYS CLI to install Mingw-w64, which contains the tools.

MSYS2 is installed, but g++ and gdb are still not found

You must follow the steps on the MSYS2 Website and use the MSYS CLI to install Mingw-w64, which contains those tools.

Next Steps
  • Explore the VS Code User Guide.
  • Review the Overview of the C++ extension.
  • Create a new workspace, copy your .vscode JSON files to it, adjust the necessary settings for the new workspace path, program name, and so on, and start coding!


To see other Tutorials related to Installing Visual Studio Code for C/C++ Language, please see here (in Indonesian). And to see this Article in Indonesian, can be see in here.

Thank you, and hopefully useful šŸ˜„šŸ˜ŠšŸ‘ŒšŸ‘ :)

Wassalamu‘alaikum Wr. Wb.

Ads