C++ difference between library and headers file

In this post we will see the C++ difference between library and headers file.

C++ difference between library and headers file

Library

Library is a pre-compiled program, while header file is an interface of the library. Library is a program that is ready to 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 in 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 the 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 allows the use of C library. However, through binding a library written in particular language can be utilized with various other languages.

Library are of two types static and dynamic library.


Header file

A Header file serves as an interface for the library. It contains all the functions names that will be included in the library but not the function definition. If you want to use a function from a library then you need to include the header’s file name. 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 recognize the strlen() function.

But if you include the header’s file name <cstring> the compiler will 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 file and calls it, if it is found. If the function is not found the compiler gives an error message.

It is only through header file that we can use the library function, so they act as an interface for the library functions. Header file usually have .h extension. There is another post which decribes how to make a header’s file.

Link :C++ how to make headers file Code::blocks




Leave a Reply

Your email address will not be published. Required fields are marked *