Category Archives: Technical

Rtags completes Emacs

This post is a description of how to setup Rtags on Emacs.  The internet is has quite a few resources about Rtags’ setup, but despite that I had to go over several of them to get things up and running for me.

Rtags is ideally meant to work for C++ projects that use CMake build systems, for others there are a few tweaks that may be needed. So here is the setup that works for developers using the traditional GNU Make for building their projects.

So here goes my setup:

To start with I had Emacs already installed, and I use Ubuntu 18.04

Installing Clang

Fire up a terminal and execute the following commands

wget -O - | sudo apt-key add -
sudo apt-add-repository "deb llvm-toolchain-xenial-3.9 main"
sudo apt-get update
sudo apt-get install clang-3.9 lldb-3.9
sudo apt-get install libclang-3.9-dev

Installing Rtags Server

Once you have clang installed now its time to install Rtags. This can be done with the following commands

git clone --recursive
cd rtags
mkdir build
cd build
cmake ..

If the make executes with out any errors then it’s time to move forward. Once this install is complete you can start the rtags server with the rdm command

rdm -j3 &

If this does not work ensure that $PATH has the path to your rtags/build/bin folder which was created in the above steps, added to it. The rdm process is located in that folder.

This will have the rdm process running and listening to build commands and create the cache for the code navigation.

Install The Rtags plugin for Emacs

Emacs being an awesome editor all you have to do is

M-x package-install RET rtags RET
M-x package-install RET flycheck-rtags RET
M-x package-install RET company-rtags RET

Now in your .emacs file add the following(sourced from here)

(defun setup-flycheck-rtags ()
  (flycheck-select-checker 'rtags)
  (setq-local flycheck-highlighting-mode nil)
  (setq-local flycheck-check-syntax-automatically nil))

;; only run this if rtags is installed
(when (require 'rtags nil :noerror)
  ;; make sure you have company-mode installed
  (require 'company)
  (define-key c-mode-base-map (kbd "M-.")
    (function rtags-find-symbol-at-point))
  (define-key c-mode-base-map (kbd "M-,")
    (function rtags-find-references-at-point))
  ;; install standard rtags keybindings. Do M-. on the symbol below to
  ;; jump to definition and see the keybindings.
  (setq rtags-autostart-diagnostics t)
  (setq rtags-completions-enabled t)
  (push 'company-rtags company-backends)
  (define-key c-mode-base-map (kbd "<C-tab>") (function company-complete))
  ;; use rtags flycheck mode -- clang warnings shown inline
  (require 'flycheck-rtags)
  ;; c-mode-common-hook is also called by c++-mode
  (add-hook 'c-mode-common-hook #'setup-flycheck-rtags))

If you use the CMake build systems the story kind of ends here, but as I said if you use the GNU Make then hold on .

A Secret Recipe for GNU Make users

Go back to the rtags folder where we synced the code from github. Remember that in step 1 we created a build folder to build rtags and that the binary rdm was present in rtags_path/build/bin

What we are going to do is use the rtags wrappers for g++/gcc to ensure that when these commands are triggered rdm process will listen and create the cache needed for it to work.

So in a terminal we will do the following

ln -s /path-to-rtags/bin/ /path-to-rtags/build/bin/g++
ln -s /path-to-rtags/bin/ /path-to-rtags/build/bin/gcc

The paths to these newly created symbolic links should already be a part of $PATH from step 1.

After doing this step open a new terminal and type:

which g++

If this returns the path to the newly created links then you are set.

Once this is done go to your project folder and build the project. If you have the terminal with rdm running you can see it is logging some information. Once this stops. You are all set. Start Emacs and open a source file. The key bindings for each action can be found on the C++ menu as shown below.


Here is a short demo of how to navigate the code using rtags


Hope you found this useful.

The Curious Case of Preprocessors

What would be the output of the following code snippet:

#include <stdio.h>
#define V 1
int main()
  int Value = 1;
  #if V == Value
      printf("Hello world\n");
  #endif // V
  return 0;

If you guessed that the program will print “Hello world” you are wrong.

The reason being #if is a preprocessor directive and at the stage where preprocessor directives are evaluated or expanded, ‘int Value = 1’ has still not been processed and memory has not yet been allocated. This will happen only at the compilation stage and not the preprocessing stage.

So basically there is no meaning in V==Value and this will evaluate to false. So for this piece of code to work you will have to define even ‘Value’ with the #define statement.

Thanks to two of my colleagues Rakesh and Mitul for this interesting discsussion.

The Garden Pi

This is a small project that will introduce you to IoT and on how to use WebIOPi and Raspberry Pi to create a small and cool project.

These are the components that you will require for this project:

  1. Raspberry Pi B+ ( Any Rpi should work)
  2. 3-6V Submersible Mini water pump
  3. Wifi Adapter  (You can skip this if you have a LAN cable long enough to reach your garden)
  4. Transistor Bs550
  5. 5V Relay
  6. Tube to supply water from the pump to the flower pots.

Setting Up the Wifi Adapter

As mentioned earlier if the LAN cable is long enough to reach your garden then you can skip this stage.

Make sure the Wifi adapter you have is compatible with the Raspberry Pi you have. I have used an Edimax Wifi usb adapter.

Insert the adapter into the Rpi USB slot and power up your Rpi.

At the command prompt type in the command lsusb


From the output of this command we get to know if the Wifi adapter was detected or not. In my case the last line shows my Wifi adapter.

The next task will be to edit the /etc/network/interfaces file. Type the command

sudo nano /etc/network/interfaces . This should open up the file and the contents should be edited as shown below:

# The loopback interface
 auto lo
 iface lo inet loopback
auto eth0
 iface eth0 inet static
 address   #your static IP
 gateway     #your gateway IP

allow-hotplug wlan0
auto wlan0
iface wlan0 inet static
 address   #your static IP
 gateway   #your gateway IP
 wpa-ssid "SSID of Your Wifi Network"
 wpa-psk "password of your Wifi Network"

Here I have configured the static IP of for LAN connection on Rpi and for Wifi on RPi.

Type ifconfig at the comamnd prompt and make sure the IP address of the Wifi (or LAN) is assigned correctly. If not reboot your Rpi.

Once this is done try pinging your Rpi from within the same network. Use the IP address of LAN or the Wifi adapter based on how your Rpi is connected to the network.

Now on your Rpi try pinging some external website like

If the ping is successful all is fine. If not don’t worry there is a fix to it.

Type in route -n

Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface   U         0 0          0 wlan0

If this is the output then notice that the gateway has not been set properly. All you have to do is execute the command given below.

sudo route add default gw

Now the you should be able to ping external websites. It would be a good idea to add this line to the ~/.bashrc file so that every time your Rpi starts up this get done automatically.

Once the wifi is setup you are ready to move on to the next section.

Installing WebIOPi

Since the WebIOPi has a pretty neat tutorial on how to setup WebIOPi, I will redirect you to that:

If you have both eth0(LAN)  and Wifi(wlan0) configured you may run into and issue where weaved is not able to access your WebIOPi server.

In that case type in sudo ifconfig eth0 down and that should do the trick. If it does consider adding this also to the ~/.bashrc file.

Setting Up the Garden Pi Web Page

For this project all you have to do is to write a single index.html file.

so create a project directory structure as shown below:

mkdir ~/gardenpi/html


edit the /etc/webiopi/config file

  • Locate [HTTP] section, add following line to tell WebIOPi where to find your HTML resources
doc-root = /home/pi/gardenpi/html

More details about the config file can be found here

Now create the file ~/gardenpi/html/index.html

Add the following content into it:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <title>Garden Manager</title>
 webiopi().ready(function() {
 // Create a "Pump" labeled button for GPIO 3
 var button = webiopi().createGPIOButton(3,"Pump");
 // Append button to HTML element with ID="controls" using jQuery

 // Refresh GPIO buttons
 // pass true to refresh repeatedly of false to refresh once
 <style type="text/css">
 button {
 display: block;
 margin: 5px 5px 5px 5px;
 width: 160px;
 height: 45px;
 font-size: 24pt;
 font-weight: bold;
 color: white;

 #gpio3.LOW {
 background-color: Black;

 #gpio3.HIGH {
 background-color: Blue;

Turn on the pump by clicking the button below
</body> </html>

Once the file is created connect and LED between the GPIO3 and Ground of the RPi.

Open up a browser in your PC or mobile and go to

and ensure that the LED turns on and off at the click of the button. All you have to do now is to connect the pump instead of the LED.

Since the pump draws more current than compared to what the Rpi can supply we will be using a relay.

Wire up your components as shown in the diagram below:


This is my Rig, you may not be able to make much from the picture:


Hope you guys ill enjoy doing this project.


My Experiments with Boost Graph Library

Recently I picked up “An Introduction to Graph Theory” by Robin J. Wilson. After going through a couple of chapters I felt the sudden urge to try out a few of graph theory problems through programming. To try out the algorithms I first need to create the graphs themselves, which seemed a bit of a tedious task. But then thats what the boost graph Library is there for.

The BGL has made it very easy to create graphs and perform any operations on them.

Below is a simple program that lets you create a graph using the BGL.


int main(){
 boost::adjacency_list<> graph;
 int noVert = 0;
 int start=0,end=0;
 char choice = 'y';

 while(choice == 'y') {
 std::cout<<"Enter the start and end vertex of the edge: ";
 std::cout<<"Do you want to add another edge(y/n): ";

 return 0;

The coolest feature of BGL is that it allows you to write your graph as ‘dot’ file, which can be used to by Graphviz to visualize. Checkout my github repo for more BGL based programs.

Range Based For Loops in C++11

Here is a short post on the range based for loops in C++. I recently learned about range based for loops in C++11 standards. These are nothing but easier constructs on writing loops. You no longer have to write:


The syntax is:


Although this looks like a very simple feature there are a few things to keep in mind while using the range based for loops.

Lets take a look at a simple piece of code:

int count(int array[100])
 int total=0;
 for(auto num:array)
 return total;
int main()
 int array={1,2,3,4,5};
 return 0;

If you compile the above code it throws compiler errors.

The same code will work if ‘array’ is a local variable in the main.
This baffled me for quite sometime and it is a friend of mine who explained to me why this happens.

This is how a range based for loop works:

It starts from the beginning of the array and travels till the end. So it needs the iterators to traverse the given array. In the code provided above ‘array’ is passed as a pointer and it does not have any iterator or boundary information. So in my original code if I pass ‘array’ as a ‘std::vector<int>array’, the code works fine.

Hope this will help someone else who is stuck. To know more in depth about how range based for loops work in C++, refer to the link provided below:

Calling a function On termination of main() in C

Dear readers, A friend of mine taught me this new “trick” where you can call a function when the main() in C exits.

In the Library “stdlib.h” there exists a function

int atexit(void (*func)(void))

the “func” is any function without any arguments.

The atexit() calls the function that you register as soon as the main() terminates.

Here is a simple sample code to demonstrate this.

void func()
printf(“This is after main\n”);
int main()

printf(“in main\n”);
return 0;

The function can be registered anywhere in the main() but it will be called only after the main() terminates.

Hope this helps atleast some of you in doing some cool things. Please feel free to share your ideas.

The World of Dynamic Programming

Recently I came across the an algorithm that can find the maximum sub array in a given 1-D array. Its called the Kadane’s algorithm.It comes under the domain of dynamic programming. I would like to write about a few things I understood about Dynamic programing so that it may help someone else to get a start and at the same time to improve my own knowledge on the topic.

Dynamic programming is an optimization problem or a programming technique, where the given problem is split into sub problems and so on.
To understand more about dynamic programming we need to look at how it obtains optimization. Dynamic programming builds its optimal solution by creating optimal solutions for its sub problems.This is also called optimal substructure.
It follows the principal of Optimality which states that “A problem is said to satisfy the Principle of Optimality if the sub solutions of an optimal solution of the problem are themselves optimal solutions for their subproblems.”

Or to put it in simpler terms optimal substructures means that the optimal solution to a problem is made up of optimal solutions to its sub problems.
Another technical term used in dynamic programming is Overlapping subproblems. This means that there are a few subproblems in total and
many recurring instances of each.

Dynamic programming uses a bottom up design process. A very good example of this will be the fibonacci series generation.
Lets first look at the recursive solution to generate fibonacci series. Instances like f(1) ,f(0) repeat multiple times. If these values can be stored they don’t have to be calculated multiple times.

The left side shows the number of steps taken by a dynamic program and the right side shows the steps taken bya recursive program to calculate fibonacci series.

An example of Dp and Recursion

An example of Dynamic Programming and Recursion

It is also to be noted that dynamic programming is not the same as divide and conquer, because dynamic programming proceeds by splitting the  problem space in every possible manner.

I have tested the efficiency of the of  both kinds of Fibonacci code. And here are my findings with respect to time taken for running the code.

Test on Fibonacci

And below is the code used for testing this.

#include <time.h>

int fibonacci1(int n)
  int a=0,b=1;
  int i,c;
  return b;

int recfib(int n)
  if ( n == 0 )
    return 0;
  else if ( n == 1 )
    return 1;
    return ( recfib(n-1) + recfib(n-2) );
int main()
  clock_t t;
  double normaltime=0,rectime=0;
  int sample;
      t = clock() - t;
      normaltime = ((double)t)/CLOCKS_PER_SEC; // in seconds
      t = clock() - t;
      rectime = ((double)t)/CLOCKS_PER_SEC; // in seconds
  return 0;

Creating a Application Launcher in Ubuntu 14.04

Dear Readers,

I am back with a very small ubuntu tip after quite sometime. This is not something that is really new and there are many sources out there that explain how to create application launchers in Ubuntu. I needed this to create a launcher for Aptana Studio.

So I thought I will write down these steps here so that others in need can find it and also it will serve as a future reference to me.

Open a terminal and navigate to your home folder and then

cd  /.local/share/applications

In this particular folder create a file called yourapplicationname.desktop

And in this particular file add the following content.

[Desktop Entry]
Name=Name of Application
Comment= a short description
Exec=absolutepath of application
Icon=path of icon image

Save this file and search for your application in unity and there you have your launcher.

A Program to calculate value of PI

Very recently I was attending an online programming course and the Instructor happened to ask a  question,
How man of you have ever written a program to calculate value of PI ?What has happened to the world ? In our days students used to attempt at least 2-3 algorithms to find value of PI.
It was only then that I realized that I have never even bothered to think about one of the most popular problems in the history of computing.
So finally I decided to learn an algorithm to calculate the value of PI, so that next time someone asks me the same question I should not run out of words. This blogpost is for all those out there lie me who have never attempted any algorithm to find the value of PI.
Here I will be introducing one of the oldest and simplest algorithm to find the value of the value of PI accurately. The accuracy of the result depends specifically on the machine and the size of the floating point numbers alloted by the programming language.
 Liu Hui was a mathematician who lived in ancient China, in his days the value of pi was approximated to be 3. But he proved that the ratio of the circumfrence to the diameter of a hexagon inscribed in a circle was 3 therefor value of Pi had to be greater than 3.
Here we start by considering a hexagon which is inscribed in a circle with radius =1. So the area of the circle is PI. Let the side length of the hexagon be 1. Then
 k2n =√(2+kn)
Sn =√(2-kn)
Area of the polygon = (1/2)(nSa)
 as the number of the sides of the polygon increases the value of a=radius.
So as the value of n increases we get a more and more accurate value of PI.
Below is the code for the algorithm
/*Author Vineeth kartha*/


int main()
  double k=1,n=6,S=0,N;
  int i;
  long double pi;
  printf("Enter the number of iterations required : ");
  printf("The value of PI is %.10Lf\n",pi);
  return 0;

The results upto the 16th iteration seems to be correct with this algorithm.

Prepaid Manager App in Ubuntu

It was only today that I put into use my Huawei E303C modem into use. I had not used it for almost 6 months. And today I  plugged it in it with the IDEA 3G sim on Ubuntu 13.04. The Huawei linux drivers almost gave me a headache and finally to get the device working . Finally I had to uninstall it and I found out that Ubuntu 13.04 automatically detects my modem.

Now people out there who have already screwed up by trying to install the Linux drivers for Huawei modem, just uninstall and reinstall the usb-modeswitch and things should be perfect.

sudo apt-get remove usb-modeswitch usb-modeswitch-data
sudo apt-get install usb-modeswitch

Now after all these I still had one major problem in front of me. To know the balance that I have on my SIM I had to remove it from the modem insert it in to my mobile and check. It is then that I found this wonderful application in ubuntu called “prepaid manager”

sudo apt-get install prepaid-manager-applet

Once this is done you will have to change a few lines in an XML file.

go to /usr/share/mobile-provider-info

and with root permissions open the serviceproviders.xml file.

In this file go to the country and to the service provider you are using and add the following lines just before the closing gsm tag </gsm>

<ussd replacement=”CODE”>*USSD CODE TO RECHARGE*CODE#</ussd>

Once this is copied make sure to change USSD CODE TO GET BALANCE to the required code for me since I was using IDEA it was *125#

Till now I have not tried the top up option, if any of my readers have done that too please eel free to comment.

%d bloggers like this: