Setting Up Sublime Text for C++ Competitive Programming Environment



The Sublime Text is a popular code editor due to its simplicity, speed and extensibility. While it may not have built-in C++ support, it's very easy to set up Sublime Text for competitive programming in C++. This guide will walk you through configuring Sublime Text to provide a smooth and efficient environment for C++ competitive programming.

Steps to Set Up Sublime Text for C++ Competitive Programming

1. Install Sublime Text

First, we need to install Sublime Text if you haven't already.


  1. Go to the Sublime Text website.
  2. Download and install the appropriate version for the operating system (Windows, macOS, or Linux).

2. Install C++ Compiler

To run C++ code on your system we must have a C++ compiler installed. The setup depends on your operating system.

Windows:

Download and install MinGW (Minimalist GNU for Windows). It provides the g++ compiler for C++.

  1. Go to the MinGW website and download the installer.
  2. Install MinGW, and make sure to add its bin directory to your system's PATH (e.g., C:\MinGW\bin).
  3. We can test the installation by running the g++ --version in Command Prompt.

MacOS:

Open the Terminal and run:

xcode-select --install

This will install the necessary development tools, including the C++ compiler (clang++).

Linux:

On Linux (Ubuntu/Debian-based), install the build-essential package:

sudo apt update
sudo apt install build-essential

This will install g++ along with other necessary tools.

3. Install Package Control in Sublime Text

Package Control is an essential tool that allows you to easily install and manage Sublime Text packages.

  1. Open Sublime Text.
  2. Press Ctrl + ~ (Windows/Linux) or Cmd + ~ (macOS) to open the console.
  3. Paste the following code and press Enter:
import urllib.request, os, hashlib; h = 'cbf7c4772e6cccd91175cfed1d035070' + '8d9c623f31fe5d1de4f29d03c1fa84ec'; pf = 'Package Control.sublime-package'; url = 'https://packagecontrol.io/Package%20Control.sublime-package'; d = urllib.request.urlopen(url).read(); e = hashlib.sha256(d).hexdigest(); p = os.path.join(sublime.cache_path(), pf); open(p, 'wb').write(d)

This will install Package Control.

4. Install C++ Packages

Now that you have Package Control we can install packages that enhance Sublime Text's support for C++ development.

C++ Syntax Highlighting and Snippets

To improve your experience with C++ in Sublime, you can install packages for syntax highlighting, snippets, and more.

  1. Press Ctrl + Shift + P (Windows/Linux) or Cmd + Shift + P (macOS) to open the Command Palette.
  2. Type Install Package and select the Package Control: Install Package option.
  3. Search for the following packages:
    C++ Snippets - Provides useful C++ code snippets.
    C++11 - Adds C++11, C++14, and C++17 support to the editor.
  4. SublimeClang - An advanced C++ syntax checker that uses clang to show errors and warnings in real time.
  5. EasyClangComplete - Provides auto-completion for C++ and other languages.
  6. Install the desired packages.

5. Set Up Build System for C++

A build system in Sublime Text allows you to compile and run C++ programs directly from the editor. You need to create a custom build system for C++.
Go to Tools ? Build System ? New Build System.
A new file will open. Paste the following code:

Windows (MinGW):

{
"cmd": ["C:\MinGW\bin\g++.exe", "-std=c++11", "-o", "${file_path}/${file_base_name}", "${file}"],
"file_regex": "^(..[^:]*):([0-9]*):?([^:]*):?([^:]*):?([^:]*):?([^:]*):",
"selector": "source.c++, source.cpp",
"shell": true
}

MacOS and Linux:

{
"cmd": ["g++", "-std=c++11", "-o", "${file_path}/${file_base_name}", "${file}"],
"file_regex": "^(..[^:]*):([0-9]*):?([^:]*):?([^:]*):?([^:]*):?([^:]*):",
"selector": "source.c++, source.cpp",
"shell": true
}

Save the file as C++ Build.sublime-build.
This build system tells Sublime to use g++ (or clang++ on macOS) to compile the C++ file. The -std=c++11 flag ensures that the C++11 standard is used. The output executable is placed in the same directory as the source code with the same name (minus the extension).

6. Build and Run C++ Code

To compile and run your C++ code in Sublime Text:

  1. Open your C++ source code file.
  2. Press Ctrl + B (Windows/Linux) or Cmd + B (macOS) to build and run the code.
  3. The output will appear in the bottom panel of Sublime Text.

Conclusion

The Sublime Text is an excellent choice for competitive programming because of its lightweight nature and customizability. By installing the right packages setting up a build system and configuring keyboard shortcuts we can create an efficient C++ development environment that will serve you well during coding competitions.

Updated on: 2025-01-20T13:05:02+05:30

161 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements