Blog Archives

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 - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
sudo apt-add-repository "deb https://apt.llvm.org/xenial/ 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 https://github.com/Andersbakken/rtags.git
cd rtags
mkdir build
cd build
cmake ..
make

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 ()
  (interactive)
  (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.
  (rtags-enable-standard-keybindings)
  (setq rtags-autostart-diagnostics t)
  (rtags-diagnostics)
  (setq rtags-completions-enabled t)
  (push 'company-rtags company-backends)
  (global-company-mode)
  (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/gcc-rtags-wrapper.sh /path-to-rtags/build/bin/gcc-rtags-wrapper.sh
ln -s /path-to-rtags/bin/gcc-rtags-wrapper.sh /path-to-rtags/build/bin/g++
ln -s /path-to-rtags/bin/gcc-rtags-wrapper.sh /path-to-rtags/build/bin/gcc

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

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.

emacs_rtags

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

rtags_demo

Hope you found this useful.

Advertisements

Thats How We C


Two and a half years back, while I was still In college that I got this idea to write a book on Programming. By then I had spent almost 4 years learning and coding a couple of programming languages. I knew I was not an expert in the field, but I also knew that I could help a few who were struggling in programming. The very next semester had a course on C programming and I knew most of my classmates would need help, so I set out on this new venture to bring out text that will help people with no background in programming to understand what C programming is all about. It can never be considered as a replacement to any of the great books out there but still, I knew that It might help someone. So after almost six months of work I was able to have a printed copy of “Thats How We C”, I sold 25 copies of the book to my classmates at no profit, I charged them with the printing cost only.
I hope many more will find this useful and if you do please do let me know, because it will be an encouragement for me to write my next book.
Also please do give me any feedback that you might have.
You can download the book from here

Classification Of Software Bugs


Who is not afraid of bugs ? Most of my readers I know will shudder just at the thought of those creepy things crawling all over you .

Just like the tag line of the Movie Eight-Legged Freaks , “Do you hate spiders? Do you REALLY hate spiders? Well they don’t like you either”

But there is another class of bugs that can ruin your sleep, your peace of mind and can really make you go nuts, we engineers call the software bugs. They can make a beautiful program a mess and can get you right back to square one. Everyone who has written a program no matter how basic it is will have encountered bugs in the first trial. Bugs can be caused due to carelessness from the part of the programmer or the programmers inability to spot out an invalid condition and it can also be caused by hardware imperfections.

No matter how much care is taken to write a bug free software is an unrealized dream and software experts claim that it will remain unrealized. Software Bugs have caused alot of trouble. Paul Bourdeaux mentions Top Ten Most Infamous Software Bugs Of All Time in his blog.

“Debugging is like removing needles from the haystack” It can be one heck of a job to find bugs in a very large program. While working on my Btech Project I encountered a Similar situation the program started giving weird results I spent almost two days checking the logic and analyzing it and the real cause of the error was a wrong argument to a function.

Despite all this There has been an effort to classify bugs in an interesting way. All those who have learned programming might have heard terms like run time errors, Logical bugs, arithmetic bugs, syntax errors etc.

But there is another classification of bugs and these are :

Bohrbug

A BohrBug is just your average, straight-forward bug. Simple like the Bohr model of the atom: A small sphere. You push it, it moves. BohrBugs are reproducible, and hence are easily fixed once discovered. These are named after Niels Bohr, who proposed a simple and easy-to-understand atomic model in 1913. In Bohr’s model, things like the path and momentum of an electron in an atom are predictable.

Heisenbug

A bug that disappears or alters its behavior when one attempts to probe or isolate it. No matter how much time and effort is spent trying to reproduce the problem, the bug eludes us. Such bugs were named Heisenbugs, after Werner Heisenberg, who is known for his “uncertainty principle”. According to his theory, it is not possible to accurately or certainly determine the position and velocity of an electron in an atom at a particular moment.

Mandelbug

When the cause of the bug is too complex to understand, and the resulting bug appears chaotic, it is called a Mandelbug. These are named after Benoît Mandelbrot, who is considered the father of fractal geometry (fractals are complex, self-similar structures). A bug in an operating system that depends on scheduling is an example of a Mandelbug.

Schroedinbug

Sometimes, you look into the code, and find that it has a bug or a problem that should never have allowed it to work in the first place. When you try out the code, the bug promptly shows up, and the software fails! Though it sounds very uncommon, such bugs do occur and are known as Schroedinbugs. They are named after the scientist Erwin Schrödinger, who proposed that in quantum physics, quantum particles like atoms could exist in two or more quantum states.

By observing the problem in the code, you change the outcome — either the software works or breaks. So these kinds of bugs are known as Schroedinbugs.

Fractal Bugs

A bug, after which its resolution is found, reveals additional self-similar bugs elsewhere in the code, after which they are fixed, likewise appear elsewhere still.

Next time time you write a program and if it crashes unexpectedly you can tell your teacher or your friends that you have a heisenbug or a bohrbug in your program, let them figure out what the heck that means. Happy Hacking 🙂

References

[1] S.G Ganesh http://www.linuxforu.com/2010/10/joy-of-programming-types-of-bugs/

[2] The Jargon File http://www.catb.org/jargon/html/index.html

Recursion In Programming


Recursion and recursive functions are a great concept in programming languages. For a beginner it can turn into one of the worst nightmares but once you get into the spirit of programming then writing recursive programs will be a bliss. Recently some of my friends found it difficult to write a recursive program to print the Fibonacci series upto a limit. This is what inspired me to write a small post on recursive functions. My post will be based on C programming. I am not an expert to write a large post on recursive functions but I would like to share with you some interesting things I know on recursion.

Recursion is nothing but the breaking up of a large problem into smaller problems. It is a divide and conquer policy. Here the smaller programs are defined in terms of the larger program, or we can say that a single function calls itself till a base condition is reached.

A recursion

A recursion

This picture should serve as an example as to what recursion is (How to make such a picture I will explain in some other post)

All recursive functions consist of two parts

  • A base case
  • A recursive step

It is the recursive step that is defined in terms of itself and it must move closer to the base case. If there is no base case or the recursive step does not reach the base case the program may run infinitely or till it runs out of memory. Almost all iterative programs can be done as a recursive program. Another classical example of solving a problem with recursion is the tower of Hanoi. Read more about it here http://familycoding.com/2012/04/12/tower-of-hanoi/

Recursive functions can make your program look more shorter and neater. But debugging and understanding a recursive program can be a difficult task. But the trick is to identify the base case and the recursive steps.

Here I will demonstrate the Fibonacci series example with and without recursion. Before you look into the program I suggest reading http://braintenance.blogspot.in/2011/08/fibonacci-numbers-math-meets-mysticism.html to really understand the fun in Fibonacci series.

The Code below shows Fibonacci series Without recursion

#include<stdio.h>
int main()
{
  int a=0,b=1,c,i,n;
  printf("Enter the number of elelments of fibonacci series needed\n");
  scanf("%d",&n);
  printf("\n %d  ",b);// to display the initial 1 of series
  for(i=0;i<n-1;i++)
    {
      c=a+b;
      printf("\n %d  ",c);
      a=b;
      b=c;
    }
  printf("\n\n");
  return 0;
}

The Code below shows Fibonacci series with recursion

#include<stdio.h>
int main()
{
  int n,a=0,b=1;
  printf("\n Enter the number of elements of Fibonacci series to be displayed = ");
  scanf("%d",&n);
  printf("\n %d  ",b);// to display the initial 1 of series
  fib(a,b,n-1);
  printf("\n********************************************\n");
  return 0;
}
int fib(int a,int b,int n)
{
  if(n!=0)
    {
      printf("%d  ",(a+b));
      n--;
      fib(b,a+b,n);
    }
  else
    {
      return 0;
    }
}

So much has been said about Recursive functions but any post on recursion would be misleading if I leave out the following points.

  • Recursive programs are less efficient as more memory space is used each time a function is called.
  • Iterative programs are less time-consuming and easier to write.

Despite that recursion can be fun and can really turn you on if you are a coding maniac.


A CGPA calculator for Kerala University Btech(2008 Scheme)


This C program calculate the CGPA if the GPAs of each semseter is Entered. Compile and run this program. This caclculator is based on Jerala University Btech 2008 scheme. This program was done for pure fun, I had a php version but as I formated my system lost it.

/** By Vineeth Kartha released under GPL**/
#include<stdio.h>
int main()
{
  float gpa[8],cgpa=0,intgpa=0;
  int crd12=58,crd=29,crdtot=0;;
  int sem;
  int i;
  printf("Enter number of semsters till which CGPA is to be calculated :  ");
  scanf("%d",&sem);
  if(sem==1)
    {
      sem=2;
    }
  while(sem<1||sem>8)
    {
      printf("Please Enter valid number of  semesters(1-8)");
      scanf("%d",&sem);
    }
  for(i=2;i<=sem;i++)
    {
      if(i==2)
    {
      printf("Enter the GPA of S12:  ");
      scanf("%f",&gpa[0]);
      gpa[1]=0;//since S12 mark is taken twice.
      gpa[0]=gpa[0]*crd12;
      gpa[1]=gpa[1]*crd12;
      crdtot=crd12;
      intgpa=gpa[0]+gpa[1];
    }
      else
    {
      printf("Enter the GPA of S%d:  ",i);
      scanf("%f",&gpa[i-1]);
      gpa[i-1]=gpa[i-1]*crd;
      crdtot+=crd;
      intgpa+=gpa[i-1];
    }
    }
  cgpa=intgpa/crdtot;
  printf("\n The CGPA till Semster %d is   %0.2f\n",sem,cgpa);
  return 0;
}
%d bloggers like this: