Getting started with C++ boost library

Last few days were well spent with the C++ library boost. It was a great learning experience. I was introduced to the one of best-kept code and some new coding paradigms. Along with that it was the first time I was coding with the new standards of C++11 and C++14.

During the time I learned a lot of things which I would like to discuss in this post. I also want to share the link of GitHub that everyone can follow for the code that I will talk about in a moment.

Here is the link: https://github.com/singh1114/parser/

As this is the first of the long lasting series about C++ I want to keep it as simple as I can. I will be discussing the various problem that one faces during his/her journey of the boost library. I would discuss this by using boost::spirit for an example.

Prerequisites:

  • C++. Of course, one must know some procedural level C++. If the reader has some knowledge about polymorphism using standard template library, that would be a +1.
  • This tutorial is only for the Linux user with a good knowledge of using compiling commands on the terminal.

So let’s start:

Installation:

$ sudo apt-get install libboost-all-dev

Use the above command in the terminal. This will install boost library. Find the boost library by using the locate command by using the command locate.

$ locate boost

This will tell you where the boost library is located which is further going to be used while applying the command to compile.

For me, the boost library was present at:

/usr/include/boost/

 

Now grab any code from the GitHub, using the following procedure:

First of all fork the git repo, then copy the new link produced in the URL bar, Then apply this command in the terminal.

$ git clone https://github.com/your_gh_username/parser/

After this, you need to try the simplest parser which is the simple calculator. For more information about basic calculator read the readme file in the simple_calcuator directory.

Use the following commands for this purpose.

$ cd parser
$ cd simple_calculator
$ g++ -I /path/to/boost/ -std=c++11 ascii_char.cpp -o example

Now execute the compiled file:

$ ./example

g++ is the originated from GCC which stands for GNU compiler collection. This is the compiler which is used to convert C++ code to machine code and create the output in the file which is named after the file given as a parameter with -o flag. -I flag stands for includes and it includes the file which is passed as a parameter. After that, we are telling that we are going to use the standard of C++11. If we don’t use -std flag then the file won’t be compiled as we are using some code that was recently added to the compilers.

The next flag describes the name of the file that needs to be compiled and so on.

On execution, you can give any input as a string to add, subtract, divide and multiply.

2-2 //This is a valid option

This will compute the result to 0. The parser doesn’t seem to be doing much but this is the start. As they say,

Every big thing start with something small.

Now I would like to take off. If you have some doubt about the code or anything related to the post, feel free to drop a comment.

BakaPlan : Baithne ka plan (Generate Seating Plan)

Hello everyone,

Sir said this before giving the task to me.

Reading other’s code is very important task that every CS engineer must know. And BaKaPlan is the software that is written by following all the coding standards. You must always follow these standards.

In this post I am going to discuss about the BaKaPlan. This task was assigned to me by sir. He wanted me to read about this software and make it work on my system. He also wanted me to read about the code and make a list in the way it works.

In this post I will write the point on which I have reached in the process.

The specific and the tasks which I have completed during the process that I really want to discuss are :-

  • Running CGI on the localhost server.
  • Running CGI in the public_html folder.
  • Configuring exim4 mail server

During this problem I faced a lot of problems and learned a lot of things. I solved these problems by the help of seniors and other trainees in the group. Some of which I would like to discuss here :-

  • what to do when your GUI doesn’t work.
  • The things that didn’t worked for me while

So, let’s start with the BaKaPlan….

This is the software that can be used to make the seating plans for an examination. This software also provides some of the other options that we can work with to come up with a better solutions for all the seating problem. We will talk more about the things that it provide but first of all we will discuss the things that are required to make this software work on our system(your system : If you are doing it with this post).

Requirement #1 : Apache server

Most of the people may already have this thing in the local system but if you haven’t got it use this command:

sudo apt-get install apache2

Requirement #2 : GNU G++ compiler

sudo apt-get install g++

Requirement #2 : CGICC library

sudo apt-get install libcgicc-dev

It’s time to create a branch of this post. This branch will tell help you to make your CGI work at the default directory in the system. For Ubuntu this place is /usr/lib/cgi-bin/

Read the post by clicking here.

Now it’s time to make it work on the place where it is required for this software i.e. In the home inside the public_html folder.

To know more about this process read this post.

The next process is pretty straight forward and can be done by following this link.

https://github.com/GreatDevelopers/bakaplan/

After this we have go to the installation procedure. The installation process is done by cloning the bakaplan repository and configuring the database. For this you can go to the link below.

https://github.com/GreatDevelopers/bakaplan/blob/master/INSTALLATION.txt

Now on going to the link the bakaplan page shows up but I am unable to log In. Every time I tried to log In to the system using the data given in the files it says that you need to login to the website and when I register to the website it says check your e-mail. When I check the e-mail it doesn’t show anything about the bakaplan. I have checked everywhere, even in the spam folder.

I put the mail regarding this issue into the mailing list but no reply has came so far.

There were some other things tried by me before putting the question in the mailing list. For that I will come up with next post. So, read the next post in this category.

If you have any doubt in this section please comment below.

CGI : Make it work for public_html directory

Hello everyone,

In this post we will discuss the way in which we will tell the server that we will place our output files in a directory inside the home folder and that output files must run on the particular link.

So, let’s start the process. First of all we need a directory named public_html within which there should be a folder named cgi-bin in the home directory. The command for this process is,

mkdir -p ~/public_html/cgi-bin/

-p flag is used to create the directory no matter what and ~ sign represents the home directory.

Now as the folders are created we need to put some dummy file inside them and check out the results in the browser to make sure that everything is working properly. Go to the file.

$ cd ~/public_html/cgi-bin/

$ vim test.cpp

Now add this content to the file. Read this post if you want to know more about vim.

#include <iostream>
using namespace std;

int main ()
{

cout << “Content-type:text/html\r\n\r\n”;
cout << “<html>\n”;
cout << “<head>\n”;
cout << “<title>Lello World – First CGI Program</title>\n”;
cout << “</head>\n”;
cout << “<body>\n”;
cout << “<h2>Lello World! This is my first CGI program</h2>\n”;
cout << “</body>\n”;
cout << “</html>\n”;

return 0;
}

Save this file with the name test.cpp or with any other .cpp extension.

Now if you are following the BaKaPlan tutorial, you will be having G++ installed in your system but before compiling this file we need to complete some steps that are important to configure this public_html folder.

$ sudo a2enmod userdir

Which mean apache2 enable mod. If you want to look more into this matter you can open the manual by using this command.

$ man a2enmod

When a2enmod command is used two new files are added in the list in the directory modes-enabled. The opposite command is a2dismod which means apache2 disable mode. when using terminal type these commands.

$ cd /etc/apache2/mods-enabled

$ ls

You will see two files added to list at the end named userdir.conf and userdir.load. These files helps in the cofiguration of the public_html folder. Now you must restart apache2 by using this command.

$ sudo service apache2 restart

Now you need to give permissions to the public_html folder. Use this command to give read permissions to others.

$ chmod -R 755 ~/public_html
$ sudo a2enmod cgi

$ sudo a2enmod cgid

$ sudo service apache2 restart

$ cd ~/public_html/cgi-bin/

$ cd /etc/apache2
$ sudo service apache2 restart
$ sudo apt-get install libcgicc-dev
$ sudo vim httpd.conf

Add this content to this file.

ScriptAlias /cgi-bin/ /home/*/public_html/cgi-bin/
<Directory “/home/*/public_html/cgi-bin”>
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
SetHandler cgi-script
Order allow,deny
Allow from all
</Directory>

Now restart apache again

$ sudo service apache2 restart

Now it’s time to compile the program. If you don’t give any output name than compiled data will be saved in a file named a.out but you can give a name using -o flag.

$ g++ test.cpp -o test.cgi

test.cgi is the name of output file.

Now in the browser type this link by replacing username with your username.

http://localhost/~USERNAME/cgi-bin/a.out

If the output file shows itself on the browser then it’s working. If not then you might have done something wrong. Follow the procedure again and solve the problem.

Still facing problem please comment below.

 

CGI : Make it work in the default directory

Hello everyone,

If we want to make CGI work, we first must know that why is it used.

To know about the prerequisites read this post. 

CGI stands for Common Gateway Interface. CGI help you to compiled program output through the browser window. If we don’t have any tool to show the output of the program on the browser then output file(obj file in the case of C program) will be downloaded. Also CGI is not only used in the case of C or C++ but it can be for many other languages.

Also before going further into the topic I want to discuss some more about how the apache2 server works. This server recognizes a directory as its default working directory and shows out any html page in this directory which have read permissions to others. As when working with the browser we are working as others. This command can be used to open up the folder that folder of the server. Just add a file

cd /var/www/apache2/html/

Now if we want to show the output of the file on the browser through localhost then we must put the output file here or we must tell that the files in the next other directory must be treated as such to show the result in the browser.

So the procedure for this is explained on two of my friend’s blogs. Please check these both procedures as both of them show different ways in which CGI can be configured in the default directory.

https://amisha2016.wordpress.com/2016/02/18/task-2/

https://iamjagjeetubhi.wordpress.com/2016/06/03/cgi-in-c/

There might be some mistakes in both of the posts. If you find any, then you must comment it in the there respected blogs.

Still need help please comment below.