Sarthak Gupta
May 10, 2018 · 6 min read

Hola to all the competitive programmers out there.

This article is all about “Being able to run a C++ file on your Ubuntu machine in the most effective way”. I do a bit of competitive programming aka CP, where mainly you are given a problem statement and you have to submit the solution of that problem statement in a single file as fast as possible(that’s why the name ‘Competitive’).

So, in this article, we will cover 4 different tools if you are starting from scratch.

  1. Installing a C++ compiler: g++.
  2. Installing a text editor: Sublime Text 2 or 3.
  3. Creating a suitable build system to run C++ files (C++14 build)
  4. Creating snippets for faster creation of code files. (This is the best part :P)

In each part, I’ll not go into the details of the tools I used. I’ll just cover the main points else it will become a very long article. You are free to research on your own about the steps, I’ve stated here. Google is your friend.


Installing g++:

Firstly check whether g++ is installed or not!

Run this command : g++ --version.

If an error message shows up, that means ‘g++’ is not installed. Now follow the given instructions.

  1. Open terminal Ctrl + Alt + T
  2. Add the following lines:
$ sudo apt install g++

The g++ in now installed in your system, with all the environment variable being set automatically.

To check if it’s installed correctly, open terminal and type the folllowing:

$ g++ --version

An it should display something like this:

g++ (Ubuntu 7.2.0-18ubuntu2) 7.2.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

So, your first step is done. :)


Installing Sublime Text 2 or 3

You can install either of the two version. Although sublime text 2 is called ‘stable’ but sublime text 3 in ‘beta’ version is also stable(yeah! ‘beta’ and stable, strange huh!)

Below given are two steps, follow either of them…….

Now, we will install the latest repository for sublime text.

Open your terminal and follow the steps:

  1. To start Sublime Text on Ubuntu first add Sublime Text signing key and repository:
$ wget -qO - https://download.sublimetext.com/sublimehq-pub.gpg | sudo apt-key add -
$ sudo apt-add-repository “deb https://download.sublimetext.com/ apt/stable/”

2. Once the Sublime Text repository is in place enter the below command to install it:

$ sudo apt install sublime-text

3. To open sublime text from your terminal, type ‘subl’ in terminal.

$ subl

and it will open up.

OR

do this :P

$ sudo add-apt-repository ppa:webupd8team/sublime-text-3
$ sudo apt-get update
$ sudo apt-get install sublime-text-installer

Don’t do both!


Setting up the build system for C++:

In order to run your c++ programs on sublime text, you need to configure your build system. Sublime provides a default build system, but that sometimes doesn’t work for all the problems we face in CP.

So in order to setup the build system:

  1. Open Sublime.
  2. Go to :

Tools > Build System > New Build System..

A new window will open. Paste the following in that window:

{
"cmd": ["g++", "-std=c++14", "$file", "-o", "${file_path}/${file_base_name}"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++, source.cxx, source.cpp",
"variants":
[
{
"name": "Run",
"cmd": ["bash", "-c", "g++ -std=c++14 '${file}' -o '${file_path}/${file_base_name}' && terminator -x bash -c '\"${file_path}/${file_base_name}\" ; read'"]
}
]
}

In this code terminator is being specified, but by default it is not installed on Ubuntu 16.04, so first install it using (in your teminal):

$ sudo apt-get update
$ sudo apt-get install terminator

There are couple of other options also available like xterm and uxterm , but I don’t like it personally because of certain glitches like difficulty in pasting stuff and inability to maximise window etc.

To use xterm , replace terminator -x by xterm -e . (not required actually)

For Ubuntu 17.10 and Ubuntu 18.04 LTS, terminator can be replaced by gnome-terminal .

There are tons of build systems available online, and while trying them you will find that most of them don’t work as needed, but I hope this will work. If it doesn’t then please notify me in comments.

3. Save the file with a suitable name. Eg. c++14.sublime-build

4. Now select your build system by selecting your build system from

Tools > Build System > c++14(in my case)

5. Open a C++ (.cpp or .c++) file, write a test program and press ctrl + b on keyboard. A console should be displayed. Enter input in that and get the output. Simple!

Up till now you are ready for submitting your first problem from a Linux machine. :D

Next part is snippets!!!!Yeah!


Creating Snippets

Now this is the part where you can brag about yourself in your code. BTW snippets expedite the act of writing code by providing a quick way to insert blocks of text/code that show up repeatedly in a project. They are both easy to understand and straightforward to write, making them a great tool for saving time and eliminating errors while coding.

To create snippets:

  1. Go to

Tools > Developer > New Snippet

A new window will open:

Sublime

The content you want to display, is placed in the ‘3’ line i.e b/w <!CDATA[ and ]] . You can put all your code inside this, you you want to display initially.

You can also specify all the unimportant stuff here. Some programmers write a new poem in each code, some write a fancy looking code and some even paste their text resume in this snippet(I am serious!!).

After this uncomment the ‘6’ line i.e <tabTrigger> and instead of ‘hello’ , place a string which you want to use to trigger your snippet.

2. Save this file with any name.(Eg: mySnippet.sublime-snippet)

So, whenever you’ll type that word(tabTrigger) in your document and then press ‘Tab’ key from your keyboard, the code will get pasted.

For example, this is my template:

<snippet>
<content><![CDATA[
/******************************************
* AUTHOR : SARTHAK GUPTA *
* NICK : SOPHOZAAR *
* INSTITUTION : BIT MESRA *
******************************************/
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define N 100005
#define MOD 1000000007
#define dd double
#define rep(i, n) for(int i = 0; i < n; i++)
#define REP(i,a,b) for(int i=a;i<b;i++)
#define rep1(i,b) for(int i=1;i<=b;i++)
#define pb push_back
#define mp make_pair
#define clr(x) x.clear()
#define sz(x) ((int)(x).size())
#define F first
#define S second
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//ios_base& scientific (ios_base& str);
${1:}
return 0;
}
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<tabTrigger>cpp</tabTrigger>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<!-- <scope>source.python</scope> -->
</snippet>

Here when I write ‘cpp’ in my text document,and press ‘Tab’ key, then the entire code gets pasted.

One of my friends uses this:

<snippet>
<content><![CDATA[
/*
Just Another Source code by -
__ ______
\ \ / / _ \ _ __ __ _ __ _ ___ _ __
\ \ /\ / /| | | | '__/ _` |/ _` |/ _ \| '_ \
\ V V / | |_| | | | (_| | (_| | (_) | | | |
\_/\_/ |____/|_| \__,_|\__, |\___/|_| |_|
|___/
*/
#include<bits/stdc++.h>
#define ll long long
#define MAX 1000003
#define pii pair<int,int>
#define VP vector< pii >
#define MOD 1000000007
#define mp make_pair
#define pb push_back
#define rep(i,a,b) for(int (i) = (a); (i) < (b); (i)++)
#define all(v) (v).begin(),(v).end()
#define S(x) scanf("%d",&(x))
#define S2(x,y) scanf("%d%d",&(x),&(y))
#define SL(x) scanf("%lld",&(x))
#define SL2(x) scanf("%lld%lld",&(x),&(y))
#define P(x) printf("%d\n",(x))
#define FF first
#define SS second
using namespace std;

int main(){

return 0;
}
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<tabTrigger>createmain</tabTrigger>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<!-- <scope>source.python</scope> -->
</snippet>

PRO TIPS:

  • The snippets can be also used when you require an algorithm that can be mostly used as it is. Like the code for ‘Sieve of Eratosthenes’. But I usually avoid this during practice sessions, because it hinders learning. This can be used in a contest anyways.
  • You don’t need to create your own snippets. Just see the code submissions of some really good programmers, copy the relevant code and make it your snippet. That’s how it works.
  • (Will add more in future. Suggestions are welcomed)

That’s it guys you are now ready to rock!!


Thanks to all who came to read this post. I hope this was a one-stop guide for all the people who are starting CP on Ubuntu for the first time or are returning to Ubuntu. If you found this post useful, then ‘give me some claps :P’ and share your knowledge by sharing this post.

Thanks to Pranav Raj for helping me write this post. :)

And I may update this post in future, so suggestions are most welcomed even if we have never met. I am quite friendly to people. :D

Click to buy me a coffee. :)

Sophozaar

Managed by Sarthak Gupta

Sarthak Gupta

Written by

Striving for excellence with some thoughts and code.

Sophozaar

Sophozaar

Managed by Sarthak Gupta

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade