Saturday, December 28, 2024
Google search engine
HomeData Modelling & AISetting up Sublime Text for C++ Competitive Programming Environment

Setting up Sublime Text for C++ Competitive Programming Environment

Note: The following steps assume a Windows OS environment.

Sublime Text is a text editor for coding. It has a smooth user interface and other exciting features. During the live contest, it is always favorable to compile the program in a local IDE instead of an online IDE’s.

In this article, we will discuss how to set up a Sublime Text Editor for Competitive Programming in C++ how to create our own build system in the sublime text editor.

Install Sublime Text Editor:

Download the setup file of Sublime Text from here and then install it.

Install MinGW Compiler:

MinGW is a native Windows port of the GNU Compiler Collection (GCC), with freely distributable import libraries and header files for building native Windows applications.

  • Download the MinGW file from here.
  • Install all the Basic Setup of MinGW.
  • Mark all the packages for installation.
  • Click on the Apply Changes option under the Installation tab as shown below:

Setting up the Environment

After completing the above steps, GCC is up and running. Now, the Environment Variable’s Path is to be updated. Follow the below steps for the same:  

  • Go to My Computer, then Right Click to get Properties > Advanced System Settings > Environment Variables.

  • Now under the System Variables Tab, look for Path > Click on Path > Click on Edit > Click on New.

  • Now locate the bin folder inside the MinGW installation folder in the C drive.
  • Copy the path of the bin folder. By default, the path is: C:\MinGW\bin
  • Paste this new path in the list and click OK.

Now Sublime Text is able to access g++ from its terminal as the path variable has been updated.

Creating a Build System

Sublime Text provides build systems to allow users to run external programs. Create a new build system for Sublime Text for setting up C++ compilation. 

  • Open Sublime Text editor and then go to Tools > Build System > New Build System.
  • Paste the following code in the file and save it.
  • Name the file as “CP.sublime-build“.
{
    "cmd": ["g++.exe", "-std=c++17", "${file}",
            "-o", "${file_base_name}.exe",
            "&&", "${file_base_name}.exe<inputf.in>outputf.out"],
    "shell":true,
    "working_dir":"$file_path",
    "selector":"source.cpp"
}

The above block of code is used for taking input from the file “inputf.in” and prints the output to “outputf.out”.

Setting up the Window Layout

Create three new files as shown below and make sure all of them are in the same folder.

  • file.cpp: The file for writing the code.
  • inputf.in: The file where we will be giving the input.
  • outputf.out: The file where the output will be displayed.

Now, perform the following steps:

  • Select View > Layout > Columns : 3. This will create three columns in the workspace. Move the three files into three columns.
  • Select View > Groups > Max Columns : 2.
  • Select the build system we just created from Tools > Build System > CP.

Below is the image to illustrate the same:

Ready for executing the programs:

Precompile Headers:

The compilation time can speed up by precompiling all the header files i.e., by precompiling the bits/stdc++.h header file. Perform the following steps for doing so:

  • Navigate to the stdc++.h file. By default, this file is located at: “C:\MinGW\lib\gcc\mingw32\6.3.0\include\c++\mingw32\bits”
  • Open the Power shell or the command window on the current folder. For this right-click while pressing the Shift key.
  • Run the below command to compile the header.

g++ stdc++.h -std=c++17

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments