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.

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.

#include<stdio.h>
#include<stdlib.h>
void func()
{
printf(“This is after main\n”);
}
int main()
{

atexit(func);
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.

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

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;
}

A Simple Game


Below I have given the code to a simple game that i made, I wish that some reader may try it and modify it, this game was maybe one of the big work that i have done in C. It has been released under the GPL so pls feel free to download, play, share and modify. Expecting feedbacks 🙂

This is how the game works there  is a matrix of 9 numbers 1-9, select a submatrix w for top d for right s for down and a for let, then press f to rotate the sub matrix and do so till the matrix is in order

/****************************This is a simple game*******************************************************
   Copyright (C) <2010> <Vineeth Kartha>

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation;version 2 of the License.
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   GNU General Public License for more details.
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA   02111-1307 USA

you can contact me for help at vineeth.kartha@gmail.com
*/

#include<stdio.h>
#include<stdlib.h>
void licence()
{
     printf("Copyright (C) <2010> <Vineeth Kartha> \nThis program is free software; you can redistribute it and/or modify\n it under the terms of the                GNU General Public License as published by\n the Free Software Foundation;version 2 of the License.   \n This program is distributed in              the hope that it will be useful,   \n but WITHOUT ANY WARRANTY; without even the implied warranty of   \n MERCHANTABILITY or FITNESS FOR A              PARTICULAR PURPOSE. See the   \n GNU General Public License for more details.   \n You should have received a copy of the GNU General              Public License   \n along with this program; if not, write to the Free Software   \n Foundation, Inc., 59 Temple Place, Suite 330, Boston,              MA   02111-1307 USA\n\nyou can contact me for help at vineeth.kartha@gmail.com");
     printf("\n\n Press any key to continue");
}
int main()
    {    
     system("clear");
     int x=0,y=0,i,j,flag=0;
     int row=0,col=0,count=0;
     int arr[3][3],corr[3][3];
     int t,choice=1,num=1,score=0;
     char ch='q';
     for(i=0;i<3;i++)
        {
         for(j=0;j<3;j++)
            {
             corr[i][j]=num;
            }
         num++;
        }
     srand(time(NULL));
     while(choice!=3)
     {
      system("clear");
      licence();
     printf("\n 1) Play \n 2) Help \n 3) Exit \n");
     scanf("%d",&choice);
     if(choice==1)
        {
/* To fill the array with numbers from 1 - 9 in random order*/
         while(flag<9)
            {
              num=rand()%10;            //to generate random numbers
              for(i = 0;i<3;i++)
                {
                  for(j=0;j<3;j++)
                    {
                     if(num == arr[i][j]||num==0)    // to verify if number is 0 or if its already present
                        {
                         count++;
                        }            
                    }
                }
              if(count==0)
                 {
                    arr[row][col]=num;
                  flag++;
                  col++;
                  if(col>=3)
                    {
                      row++;
                      col=0;
                    }
                }
            else
                {
                 count=0;
                }
            }
/* To display the array*/

        for(i=0;i<3;i++)
            {
             for(j=0;j<3;j++)
                {
                printf("%d",arr[i][j]);
                printf("    ");            
                }
              printf("\n");
            }

        while(1)
            {
            scanf("%c",&ch);
/* Select upper square*/
            if(ch=='w')
                {
                 x=0;
                }    
/* Select lower square*/
            if(ch=='s')
                {
                 x=1;
                }
/* Select left square*/

            if(ch=='a')
                {
                 y=0;
                }
/* Select right square*/

            if(ch=='d')
                {
                 y=1;
                }
/* Rotate the digits*/
            if(ch=='f')
                {
                 t=arr[x][y];
                 arr[x][y]=arr[x+1][y];
                 arr[x+1][y]=arr[x+1][y+1];
                 arr[x+1][y+1]=arr[x][y+1];
                 arr[x][y+1]=t;
                 score++;
                }
/* Exit*/
            if(ch=='q')
                {
                 break;
                }
            system("clear");
            printf("\n\n\n\n\t\t\t");
            for(i=0;i<3;i++)
                {
                 for(j=0;j<3;j++)
                    {
                      printf("%d",arr[i][j]);
                     printf("    ");            
                    }
                 printf("\n\t\t\t");
                }
            num=0;
            for(i=0;i<3;i++)
                {
                 for(j=0;j<3;j++)
                    {
                      if(arr[i][j]==corr[i][j])
                        {
                         num++;
                        }            
                    }
                }
            if(num==9)
                {
                 printf("\n\n\t\t\tYOU WON\n");
                }
            }
    }
    if(choice==2)
    {
     system("clear");
     printf("\n This is a game where you have to arrange numbers 1-9 in a matrix in order.\n w --> select the top sub matrix \n s --> select bottom          submatrix \n a --> select left submatrix \n d --> select right submatrix.\n f--> rotate the numbers.");
     printf("\n press any  NUMBER key to continue.......");
     scanf("%d", &num);
    }
    }
    if(choice ==3)
    {
     system("exit");
     system("clear");
    }
}
%d bloggers like this: