Category: GCC (g++)

  • Doxygen – Documentation Builder

    Doxygen

    Generate documentation from source code

    Doxygen is the standard tool for generating documentation from annotated C++ sources, but it also supports other popular programming languages such as C, Objective-C, C#, PHP, Java, Python, IDL (Corba and Microsoft flavors), Fortran, VHDL, Tcl, and to some extent D.

    Installation

    Run following command in terminal:

    $ sudo apt-get install doxygen

    Usage

    It’s very simple to use. Just type $ doxygen in terminal and you got its manual.

    For using comments format is very important. So before start check its comment instructions.

    To create documentation, move to folder where your source file exits through terminal and then type

    $ cd /path/to/your/project/source/
    $ doxygen -g [filename]

    You can fill any filename as your choice. Its configuration file and you can edit that according your project details like change project name in filename.(config file for doxygen)

    Then run

    $ doxygen [filename]

    By this your documentation will be generated. This will create 2 folders in your current directory.
    Folders:

    • html for html documentation open /path/to/project/source/html/index.html to check documentation.
    • latex for documentation using latex as pdf output. For that file run

      $ cd /path/to/your/project/source/latex
      $ make

    This will create refman.pdf file(check pdf file as file name may be changed in your case).

    That’s all. This is basic guide, you can do more by exploring filename.conf file.
    You can also add Special Commands in comments to make your documentation more descriptive.

    Hope this will help you.

  • C++ with CGI

    Before doing example, You have to configure cgi-bin with apache server.
    If its done then try your first example.

    Open an empty file and write following c++ code.


    #include <iostream>
    using namespace std;

    int main ()
    {

    cout << "Content-type:text/html\n\n";
    cout << "Hello World - First CGI Program";

    return 0;
    }

    Now save this file with .cpp extension(example.cpp) in public_html/cgi-bin folder.
    Then compile that program.
    $ g++ -o example example.cpp

    This will create example file which is executable.
    Then open browser and open following link”

    localhost/~username/cgi-bin/example

    This will show you following o/p on browser.

    Hello World – First CGI Program

    If you find any problem then share it as comments.
    Thankyou.

  • CGI Configuration on Apache

    There are 3 ways to use CGI.

    • First Way
      When we install Apache server, then /usr/lib/cgi-bin folder is automatically created. You can place c++ files in that folder and execute them on browser(localhost/cgi-bin/example).
    • Second Way
      You can also configure /var/www/cgi-bin for CGI programming.
    • Third Way
      You can also configure cgi-bin in home directory as /public_html/cgi-bin.

    According to me 3rd way is best as its in home directory and we don’t have to use sudo while compiling or creating any file.

    Steps to configure cgi-bin in public_html and public_html. Run following commands in terminal

    1. $ sudo a2enmod cgi
    2. $ sudo a2enmod cgid
    3. $ sudo a2enmod userdir
    4. $ sudo service apache2 restart
    5. $ mkdir ~/public_html
    6. $ cd ~/public_html
    7. $ mkdir cgi-bin
    8. $ cd /etc/apache2
    9. $ sudo vim sites-available/default
    10. Add following text in 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>
      
    11. $ sudo service apache2 restart
  • MySQL with C++

    Here’s an example for connecting C++ program with MySQL.

    You can simply download code from Github [Link]

    or

    Do as following

    Write following code in text editor and save file as mysql.cpp

    
    // Include Header Files
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    
    // For MySQL Connection
    #include <mysql.h>
    
    using namespace std;
    
    // Defining Constant Variables
    #define SERVER "localhost"
    #define USER "root"
    #define PASSWORD "password"
    #define DATABASE "test"
    
    int main()
    {
        MYSQL *connect;
        connect = mysql_init(NULL);
    
        if (!connect)
        {
            cout << "Mysql Initialization Failed";
            return 1;
        }
    
        connect = mysql_real_connect(connect, SERVER, USER, PASSWORD, DATABASE, 0,NULL,0);
    
        if (connect)
        {
            cout << "Connection Succeeded\n";
        }
        else
        {
            cout << "Connection Failed\n";
        }
    
        MYSQL_RES *res_set;
        MYSQL_ROW row;
    
        // Replace MySQL query with your query
    
        mysql_query (connect,"show tables");
    
        unsigned int i=0;
    
        res_set=mysql_store_result(connect);
    
        unsigned int numrows = mysql_num_rows(res_set);
        
        cout << " Tables in " << DATABASE << " database " << endl;
    
        while (((row=mysql_fetch_row(res_set)) !=NULL))
        {
            cout << row[i] << endl;
        }
    
        mysql_close (connect);
    
        return 0;
    }
    
    

    Now compile and run this program in terminal using following statements.

    $ g++ -o mysql mysql.cpp -L/usr/include/mysql -lmysqlclient -I/usr/include/mysql

    $ ./mysql

  • MySQL Connector for C++

    MySQL Connector/C++ is a MySQL database connector for C++.

    Installation

    If you are using Linux then you need to install two additional packages i.e. libmysql++ and libmysql++-dev for connecting C++ program with MySQL

    Run following commands in terminal.

    $ sudo apt-get install libmysql++

    $ sudo apt-get install libmysql++-dev

  • Graphics.h in Linux for c/c++

    graphics.h is a header file that is used to add graphics in c/c++ program in TURBOC. But in Ubuntu graphics.h file is not supported by g ++ compiler.

    For executing graphics program in g++ compiler, we have to install library files.

    Steps for installing the graphics library files are as follows:

    First run $ sudo apt-get install build-essential in terminal to install necessary compiler tools.

    Then, install the following packages:
    libsdl-image1.2
    libsdl-image1.2-dev
    guile-1.8
    guile-1.8-dev
    libsdl1.2debian-art
    libartsc0-dev
    libaudiofile-dev
    libesd0-dev
    libdirectfb-dev
    libdirectfb-extra
    libfreetype6-dev
    libxext-dev
    x11proto-xext-dev
    libfreetype6(upgrade)
    libaa1
    libaa1-dev
    libslang2-dev
    libasound2
    libasound2-dev

    You can install them by typing the following command in the terminal :
    $ sudo apt-get install libsdl-image1.2 libsdl-image1.2-dev guile-1.8 guile-1.8-dev libsdl1.2debian-arts libartsc0-dev libaudiofile-dev libesd0-dev libdirectfb-dev libdirectfb-extra libfreetype6-dev libxext-dev x11proto-xext-dev libfreetype6(upgrade) libaa1 libaa1-dev libslang2-dev libasound2 libasound-dev

    After this download libgraph-1.0.1 (This is the link) to your Home Folder.
    Right click on it and press “Extract here”

    open terminal and navigate into the folder u just extracted the file contents into by running $ cd libgraph-1.0.1

    Now run the following commands one after the another. Proceed to the next command only if the preceding command executes without any errors.
    $ ./configure
      $ sudo make
      $ sudo make install

    If you get any errors during the “sudo make” step, paste it in the comments section of this post.

    Now technically install is over. :)

    Try writing a simple program including graphics.h. Declare the values of gd and gm and call initgraph as follows

    int gd,gm=VGAMAX; gd=DETECT;
    initgraph(&gd,&gm,NULL);

    and when compiling using g++ add an extra paramter -lgraph

    i.e if you are compiling a program example.cpp in the terminal u type in

    $ g++ example.cpp -o example.o -lgraph

    And execute the program by runnung ./example.o

    If on compile it gives u an error that says “could not load shared libraries” or something like that just run the following command and it should fix it

    $ sudo cp /usr/local/lib/libgraph.* /usr/lib

    An example program(to print a line) :

    #include<iostream>
    #include<graphics.h>

    int main()
    {
    int gd,gm=VGAMAX; gd=DETECT;
    initgraph(&gd,&gm,NULL);
    line(50,50,80,80);
    delay(5000);

    return 0;
    }

  • Installation of g++ compiler in LINUX

    GNU C++ compiler is a fairly portable optimizing compiler for C++/C. g++ compiler is used to execute both c and c++ programs.

    Command for installing G++ Cmpiler

    Execute this command in terminal :

    $ sudo apt-get install g++
    
    Steps for compiling and executing c++ program
    
    1. Write and save the program in gedit
        
        //Sample c++ program
        #include<iostream>
        using namespace std;
        main()
        {
             cout<<"Sample c++ Program";
             return 0;
        }
    
        Save the program as sample.cpp (.cpp is the extension for c++ program).
    
    2. Compile the program
    
       Write the following command in terminal after installing g++ compiler.
       $ g++ sample.cpp -o sample.o
    
       In this command,
    • g++ indicates the compiler name,
    •  sample.cpp is a file name of the source program,
    •  -o option specifies the file name of the output program.
    •   sample.o is an output file.
       If there is no syntax/semantic error in you program then the compiler 
       will successfully generate an executable file,otherwise fix the problem 
       in your code.
    
    3. Executing the program
       Write the following command in terminal after compiling c++ program.
       $ ./sample.o
     
       After executing this command it gives the output as:
       Sample c++ Program
    
    If you have any problems then share it through comments.