Library and how it differs from Header’s file.


Library and how it differs from Header’s file
Library is a pre-compiled program while header’s file is an interface for the library.Library are program that is ready for use by the programmer and need no compilation.The best example of library file is .dll file(in Windows) and .so file(in Linux).In Windows,if you want to include a plugin for a software,you transfer the .dll file to the plugin directory of the software and start using the plugin.Here,you need not worry about compiling the file because the file is already compiled.All you have to do is call the correct function of the library to use it. The usage of library is language dependent.If you are programming in JAVA you can only use the library written in JAVA.This also holds true for C and C++ library but,C++ standard allowed the use of C library.However,through binding a library written in particular language can be utilized with various languages.Library are of two types static and dynamic library. For more information you can go to this link Static and Dynamic library in C++.

Header’s file serves as an interface for the library.It contain all the functions names that will be included in the library but not the function’s code.If you want to use a function from the library you need to include it’s header’s file.For example consider the program below.
 

Finding the length of the string.

#include <iostream>

using namespace std;

int main( )
{
string str=”Naruto is stronger than Luffy”;

cout<< “Length of \”” << str << “\” string is ” << strlen(&str[0]) << endl ;

cin.get();
return 0;
}

If you run the program you will get “error:strlen was not declared in this scope” .The compiler does not know what this strlen() function is.But if you include the header’s file name cstring the compiler compile the program happily and gives you the length of the string str. By adding the header’s file name cstring the compiler searches for the function strlen in the header’s file and calls it if found,if not it gives you an error message .It is only through adding header’s file that we can use the library functions, so they act as an interface for the library functions.Header’s file usually have .h extension.The next topic discusses on how to make a header’s file.


 


Making .h and .cpp file

 
You have seen that header’s(.h) file becomes necessary if you are using library.In other word,if you are making a library you also need a header’s file. Header’s file only introduced what functions your library will contain,the main code of the function is written in .cpp (for C++, .c for C )file.Here we will learn a few ways to make .h and .cpp file.

I ) If you are using Code::Blocks the fastest way to make .h and .cpp is to navigate to the left-top side of the window and go to the directory “File->New->Class” and click on it then a new window appears where the name of the class is entered(say Class_name).Clicking create will create Class_name.h inside newly created folder ‘include‘ and Class_name.cpp inside the newly created folder ‘src‘. If you open the Class_name.h the file will appear like this.

#ifndef CLASS_NAME_H ///Your class name
#define CLASS_NAME_H ///Your Class name

class Class_name
{
public:

/*Declare your variable,function name here,say I want to make a function that will add two integers*/

int add(int i1 , int i2 ) ;

Class_name( );
virtual ~Class_name( );

protected:
private:
///Declare your function name here

};

#endif // CLASS_NAME_H

 

The three statements #ifndef CLASSNAME_H , #define CLASSNAME_H and #endif are necessary.They prevent the compiler from multiple inclusion of the contents of the file.What actually happens is that when the compiler come across include/Classname.h for the first time it includes the header’s file contents in the preprocessor. But when it come across the statement “include/Classname.h” again in another file it prevents the inclusion of the content of the file as it has already been included.If the statements were not added you will get an error.

In your .cpp add the code of add() function.So the file will appear as,

#include “../include/Class_name.h”

Class_name::Class_name( )
{
//ctor
}

Class_name::~Class_name( )
{
//dtor
}

int Class_name::add(int i1 ,int i2)
{
return (i1+i2) ;
}

 
To include the add() function in your main.cpp file include ” #include “include/Class_name.h” “.So,the main.cpp file will appear as,
 

#include <iostream>

#include “include/Class_name.h”

using namespace std ;

int main( )
{
Class_name cn ;

cout<< cn.add(1,999) << endl ;

cin.get() ;
return 0 ;
}

**Note :: The header’s file name is declared inside ” “(double quotation) not under < > ‘ (sign).This allow the compiler to search for the file in the directory where your main.cpp file is present.However, if we declare it inside < > the compiler would search for the library file in the standard directory ( the directory where all the library file is store in your code::Blocks installation directory,most probably “CodeBlocks\MinGW\include” ) and you will get an error as the file cannot be found.
 
ii )Using notepad or notepad++ you can also create .h and .cpp files .

a)For header’s file add the three lines given below.

#ifndef CLASSNAME_H ///CLASSNAME is the name of your class

#define CLASSNAME_H ///CLASSNAME is the name of your class

/* Declare your class or function name here */

#endif ///CLASSNAME

I have already explain the importance of the three statements above.Now save the file in your main.cpp file directory.
 
b ) For .cpp file.
Open the notepad and include the header’s file name as given below and save it as classname.cpp in the directory of the main.cpp file.

#include “classname.h”

/* Methods or functions definition here */

 
Note that defining the functions or methods is allowed only inside .cpp file.However , you can define an inline function inside .h file because inline functions does not behave like the usual function and are meant only for code substitution.More about inline function will be discussed in Chapter 4

A program is given below using the inline method in classname.h header’s file.

classname.h#ifndef CLASSNAME_H ///CLASSNAME is the name of your class
#define CLASSNAME_H ///CLASSNAME is the name of your class

#include <iostream>

using namespace std;

class classname
{
public:
classname( );

inline void func( )
{
cout<<“func() is an inline method \n” ;

}

~classname( );

} ;

#endif ///CLASSNAME

 

#include “classname.h”

classname::classname( )
{
///Constructor
}

classname::~classname( )
{
///Destructor
}

 

main.cpp

#include < iostream >
#include “classname.h”

using namespace std ;

int main( )
{
classname cn ;
cn.func( ) ;

cin.get() ;
return 0 ;
}