Setup Sublime Text For C++

Setup Sublime Text For C++

C++ Competitive Programming

Do you want an IDE on which you just copy your test cases, run your code and see output on the same window?

Then Sublime text is for you, my friend.

We are going to set up sublime for Windows, Linux, and macOS.

Please follow the below steps for setting things up:

  1. Download Sublime Text from here: sublime
  2. Download GCC compiler:

    • For Windows setup the C++ env from GFG article.

    • For Linux:

      • Run on terminal:
        sudo apt-get update
        sudo apt-get install build-essential
        sudo apt-get install manpages-dev
        
      • To verify the installation run gcc --version, this should output which gcc version is installed.
    • For macOS:

      • Install GCC through homebrew by running brew install gcc.
      • Install gtimeout through homebrew by running brew install coreutils.
      • To verify the installation run which gcc, this should output the path where gcc is installed.
      • If you don't have homebrew install it from here homebrew
  3. Let's Divide the window into three groups (one for c++ editor, one for input, and one for output).

    • Create a directory with any name say C++ and open it in Sublime. Screenshot 2022-03-15 at 9.32.54 PM.png
    • Create three column from Layout: Screenshot 2022-03-15 at 9.38.00 PM.png Screenshot 2022-03-15 at 9.38.52 PM.png
    • Join two blocks vertically: Screenshot 2022-03-15 at 9.39.49 PM.png
    • Save upper right as input.in file.
    • Save lower right as output.in file.
    • Save left column with a cpp file extension to compile c++ code. Screenshot 2022-03-15 at 9.45.03 PM.png
  4. Open sublime text and install PackageResourceViewer to add build for C++.

    • Open Package Control. Screenshot 2022-03-15 at 8.37.35 PM.png
    • Choose Install Package. Screenshot 2022-03-15 at 8.37.54 PM.png
    • Install PackageResourceViewer by searching PackageResourceViewer PackageResourceViewer
  5. Create a c++ build file with PackageResourceViewer:

    • Open package control and search PackageResourceViewer: Open Resource. Screenshot 2022-03-15 at 8.49.32 PM.png
    • Click on C++. Screenshot 2022-03-15 at 8.52.24 PM.png
    • Open C++ Single File.sublime-build. Screenshot 2022-03-15 at 8.54.35 PM.png
  6. Change the content of build file for each OS distribution.

    • For Windows:
      {
      "cmd" : ["g++ -std=c++14 $file_name -o $file_base_name && timeout 4s ./$file_base_name<input.in>output.in"], 
      "selector" : "source.c",
      "shell": true,
      "working_dir" : "$file_path"
      }
      
    • For Linux:
      • Note I have g++-11 version at the time of writing. Please update the version which you have installed by checking with g++ --version
        {
        "cmd" : ["g++-11 $file_name -o $file_base_name && gtimeout 4s ./$file_base_name<input.in>output.in"], 
        "selector" : "source.c",
        "shell": true,
        "working_dir" : "$file_path"
        }
        
    • For macOS:
      • Copy the path of GCC by running which gcc.
      • Edit the build file:
        {
        "cmd" : ["/usr/local/bin/g++-11 $file_name -o $file_base_name && gtimeout 4s ./$file_base_name<input.in>output.in"], 
        "selector" : "source.c",
        "shell": true,
        "working_dir" : "$file_path"
        }
        
  7. Add your cpp code in c++ editor block and run program by cmd + B or ctrl + B.

Screenshot 2022-03-15 at 7.46.38 PM.png

Wola, Congratulations!!!!. Eat, Sleep, Code and Repeat.