简体   繁体   中英

Libraries vs classes in c++

I came across a terminology in C++ which is a library. The book I am reading states that iostream is a library , and it is a system library. Then After that it says in the book, "later, you will create your own library with the extension .h". Is a library the same as class because clearly, when I included my .h library, I actually had created a class. If a library is the same as a class, what can we say about in C language , is it a class ? Thank you.

Please note that this is just a simplistic explanation so you can wrap your head around it, not a pedantic or an exact and complete definition of a library.


A library is a collection of functions, classes and other stuff packaged together.

For instance the C++ standard library is (conceptually) composed of many libraries, eg:

  • string library
  • algorithm library
  • input/output library
  • etc.

The IO library contains some classes like:

  • std::iostream (actually a typedef to std::basic_iostream )
  • std::ios
  • std::istream
  • std::ostream
  • etc.

In order to use a library you basically need two things: the library headers in order to have access to the declarations and a library which needs to be linked to your project in order to have access to the symbols exported by such library. The OS comes with the C++ Standard Library preinstalled and the compiler -- when in C++ mode -- automatically links to it, so all you have to do is include the necessary headers.


In order to understand what a header is and what's it's role you first need to understand the difference between declaration and definition. You can reefer to What is the difference between a definition and a declaration? or any other reading material.

Then you need to understand the concept of compilation units. You can read What is a “translation unit” in C++ or How is compilation unit defined in c++ .

Using all the above you should to be able to compile multiple source files into one executable and understand the basics mechanism involved. You can read Using multiple .cpp files in c++ program or How to use multiple source files to create a single object file with gcc

From here there is a small step to be able to create and use your own static library. You can start here: How to create a static library with g++?

Another important concept to understand is compilation/linking: How does the compilation/linking process work?


...or alternatively all you need to know is that in order to use std::iostream you need to include <iostream> . Ultimately it's up to you how much you want to absorb as "because that's how it's done" and how much you want to understand. Progress can be made only if you chose one of the options (spoiler: it's the latter).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM