C++ Libraries: How To Install And Use A C++ Standard Library

By Sruthy

By Sruthy

Sruthy, with her 10+ years of experience, is a dynamic professional who seamlessly blends her creative soul with technical prowess. With a Technical Degree in Graphics Design and Communications and a Bachelor’s Degree in Electronics and Communication, she brings a unique combination of artistic flair…

Learn about our editorial policies.
Updated March 7, 2024

This C++ Libraries tutorial will explain what are Libraries in C++, their types along with installation and usage of a C++ Standard Library:

A library can be defined as a package of code or functions that is reusable. Various applications can use the same library again and again. By having libraries, we achieve reusability i.e. one of the principles of Object-Oriented Programming.

For example, mathematical functions like sin, cos, tan, abs, etc. can be made into a library. By doing this we don’t have to write these functions again and again for every application. We can just reference the library and reuse the code in our applications.

=> Visit Here To Learn C++ From Scratch.

C++ Library

C++ Libraries

A C++ library usually consists of:

A header file: This defines the functionality that the library is offering.

A precompiled binary: This file contains the actual implementation of the functionality. This implementation is precompiled into a machine language.

Libraries are precompiled as there is no reason to recompile the libraries as they hardly change. Secondly, as the precompiled libraries are into a machine language form, they are secured as nobody can access them directly. Thus the libraries are secured.

In C++ we have two types of Libraries:

  • Static Libraries
  • Dynamic libraries

Static Libraries

Static Libraries are also called as Archives. A static library is a library that becomes a part of the executable which references it. Thus when we develop an application and reference a static library in it, the contents of the static library are replaced in the application.

The static libraries on Windows usually have an extension “.lib” while those on Linux has extension “.a” (or archive).

One advantage of having a program reference static library is that we don’t have to distribute the static library along with the executable. This is because the contents of the static library are a part of the applications and there is no need to give it separately.

This specific advantage results in a disadvantage as the executable can become bulky and too much space is wasted as the library is a part of the executable.

Another disadvantage of having a static library is that if the static library changes, then the executable referencing it needs to be updated as well.

Dynamic Libraries

The dynamic library is also known as the “shared Library”. In Windows, the dynamic library has the extension “.dll” (dynamically linked library). On Linux, the dynamic library has the extension “.so” (shared object).

A dynamic library is a library that consists of routines that are loaded into the application at run time. When an application references a dynamic library, then the library does not become a part of the application but it remains as a separate unit.

The dynamic library has an advantage that one copy of the library can be shared by many applications thereby saving space. Yet another advantage is that, as the dynamic library is a separate unit, the application executable does not change when the dynamic library changes.

The only disadvantage that a dynamic library has is that it requires a separate program to load and reference the library which can be cumbersome. To solve this problem, we use the “import library” routine.

An import library is a mechanism that loads and uses a dynamic library automatically into the program. On Windows, the import library keeps a small static library (.lib) having the same name as the .dll file and is linked to the program at compile time.

Once this is done, the dynamic library is used as if it were a static library. On the Linux operating system, the dynamic library or the shared object (.so) file acts as an import library as well as a dynamic library. Most of the linkers on Linux OS are able to build an import library for a dynamic library.

How To Install And Use C++ Libraries

Let’s see some of the steps that we need to follow to install these libraries and use them in our application.

#1) Acquiring the Library

In order to use the library in our application, we first need to acquire the library. The library may be precompiled – in such a case we just need to download the library for the operating system. If the library is not precompiled, then we need to download the source code and compile it on our machine.

On windows, the libraries usually have .zip extension while on the Linux Operating system the library package is distributed as ‘.RPM’.

#2) Install the Library

The next step once we obtain the library is to install the library. Installation on Windows is as simple as unzipping the contents of the library in a folder. On Linux, we can invoke the package manager to install the library.

#3) Include the Library path for the Compiler

We need to set the path of the library so that the compiler knows where to look for the library files. This can be ‘include’ directory on Windows or ‘/usr/include’ on Linux.

#4) Link the Library path

This step is performed so that the linker knows the exact path of the libraries that it has to link during the linking process. Usually, there is ‘/lib’ folder on Windows or ‘/usr/lib’ folder on Linux that contains all the libraries.

Once the library is installed, the compiler and the linker know the path of the library to use, and the library is ready for use. In our program, we can refer to the library functionality, and as we have included the header files in our program, the compiler will be able to compile the functionality.

As programmers mostly use IDEs for development, we specify the library path in the IDE itself.

Standard Headers/Libraries In C++

Let’s discuss some of the standard headers or libraries the C++ language uses. We have already discussed these headers in their respective topics in our C++ tutorials.

C Standard Library

ANSI C standard specifies the C standard library or libc that acts as the standard library for the C programming language. C++ includes each header from the C standard library under a different name by adding ‘c’ at the beginning and removing ‘.h’ at the end. Thus header xxx.h in C will become ‘cxxx’ in the C++ standard.

The C standard library provides the functionality related to Macros, String handling, Mathematical computations Type definitions, Input/Output processing, Memory management, etc. among others.

Check here for more information on C Standard Library.  

General

The general C++ library includes the following headers as shown below.

Name of the HeaderDescription
< algorithm >Algorithms used by many containers are defined in this header.
< chrono > Includes time elements like std::chrono::time_point, std::chrono::duration and clocks.
< functional >This header provides functions objects to be used with general algorithms.
< iterator >Contains classes and templates for iterators.
< memory >Defines memory management functionality in C++.
< stdexcept >Defines standard exception classes including std::logic_error and std::runtime_error, both derived from std::exception.
< tuple >Defines functionality for the tuple. Available from C++ 11 onwards.
< utility >Template classes like std:: pair etc. or namespaces like std::rel_ops are defined in this header.

Streams and Input/Output

Name of the HeaderDescription
< fstream >Provides functionality for file based input-output.
< iomanip >Contains functionality for output formatting like precision, base etc.
< ios >Contains basic functionality for iostream operations.
< iosfwd >Provides forward declarations of several I/O-related class templates.
< iostream >Provides C++ input and output fundamentals.
< istream >Contains functionality of the template class std::istream and other supporting classes for input.
< ostream >Contains the functionality of the template class std::ostream and other supporting classes for output.
< sstream >Defines the template class std::stringstream and other supporting classes used for string manipulation.
< streambuf >Contains functionality for reading and writing to/from certain types of character sequences, such as external files or strings.

Language Support

Name-of-the-Header
Description
< exception >This header contains various types and functions for exception handling, including the base class for all exceptions, std:: exception.
< limits >This header contains the template class std::numeric_limits used for describing fundamental numeric properties
< new >Contains operators new and delete and other functions related to the fundamentals of C++ memory management.
< typeinfo >Provides facilities for working with C++ run-time type information.

Strings

Name of the HeaderDescription
< string >This header contains the standard string classes and templates in C++.
< regex >This is new in C++11. Provides utilities for pattern matching strings using regular expressions.

Localization

Name-of-the-HeaderDescription
< locale >This header has locale functionality. It defines classes and declares functions that encapsulate and manipulate the information specific to a locale.
< codecvt >Contains code conversion facets for various character encodings.

Containers

Name-of-the-HeaderDescription
< array >Contains functionality for std::array, a container for a fixed sized array. New addition in C++11 and TR1.
< bitset >Specialized container class std::bitset, a bit array.
< deque >Contains functionality for std::deque, a double-ended queue.
< forward_list >Header for std::forward_list, a singly linked list. New addition in C++11 and TR1.
< list >Contains functionality for std::list, a doubly linked list.
< map >Contains functionality for std::map and std::multimap, sorted associative array and multimap.
< queue >Implements the functionality for std::queue, a single-ended queue, and std::priority_queue, a priority queue.
< set >Has the header for std::set and std::multiset, sorted associative containers or sets.
< stack >Provides the container adapter class std::stack, a stack.
< unordered_map>This header contains std::unordered_map and std::unordered_multimap, hash tables functionality. New in C++11 and TR1.
< unordered_set>Provides functionality for std::unordered_set and std::unordered_multiset. New in C++11 and TR1.
< vector >Provides functionality for std::vector, a dynamic array.

Thread Support Library

Name of the HeaderDescription
< thread >A new class and namespace for working with threads from C++11 onwards.
< mutex >The new addition to C++11 onwards. It provides mutexes, locks, call once, etc. which are all the mechanisms for mutual exclusion.
< condition_variable >Contains functionality for Condition variables that provide synchronization primitives used to block a thread until notified by some other thread that some condition is met or until a system time is reached. The new addition for C++ 11 onwards.
< future >This header has the components that a C++ program can use to retrieve in one thread and the result (value or exception) from a function that has run in the same thread or another thread.

Numeric Library

Name of the HeaderDescription
< complex>The header describes various functions for representing and manipulating complex numbers. It also has a class template for complex numbers
< random>Functionality to generate (pseudo-)random numbers
< valarray>This header contains
- five class templates (valarray, slice_array, gslice_array, mask_array, and indirect_array),
- two classes (slice and gslice),
- A series of related function templates for representing and manipulating arrays of values.
< numeric>Contains general numeric operations.

Conclusion

We discussed C++ libraries in detail along with their types, and the steps to install and setup the library.

We came to know that the standard C++ library contains various headers that provide different functionalities. We can use these standard library headers to develop a variety of applications in C++.

=> Check ALL C++ Tutorials Here.

Was this helpful?

Thanks for your feedback!

Leave a Comment