3

I have just started learning cpp and one thing that is really confusing me is #include <iostream> or #include<vector>. Some people say that we are including iostream library and some say that #include is used for including header files. But iostream and vector don't have .h extension so how can they be header files? Also, can we include a library by using #include ? This also makes me think about difference between iostream.h and iostream . Which one is header file? Which one is library? If we are only including header files then why don't we write #include<vector.h>?

What does the standard cpp library contain? Smaller libraries like containers library , utilities library?

I tried looking on cppreference but couldn't understand

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
  • Those header files is just no extension, https://stackoverflow.com/questions/441568/when-can-you-omit-the-file-extension-in-an-include-directive – Puelloc Dec 20 '22 at 08:16
  • `` is a header specified (with some variations) in all C++ standards. It provides declarations of types (e.g. `std::istream` and `std::ostream`), and objects(e.g. `std::cout`). Since C++11, it `#include`s other standard headers related to I/O (e.g. ``). Like all standard headers, it is a component of the C++ standard library (specifically - its purpose is providing a defined interface for code to use parts of the library). `` has never been part of standard C++, but was a precursor to `` that predated the first C++ standard (which was ratified in 1989). – Peter Dec 20 '22 at 08:25
  • 1
    You cannot include libraries because they are not source code. `#include` always refers to a source file, the name of that file can be anything at all. An extension of `.h` is common, so is `.hpp` but the C++ standard headers do not have any extension. Programmers who say that you are including a library are using the wrong terminology. – john Dec 20 '22 at 08:25
  • cppreference is a good reference for almost everything. Overview of all libraries and their headers that make the standard library: https://en.cppreference.com/w/cpp/header – 463035818_is_not_an_ai Dec 20 '22 at 08:31
  • oh, you already read cppreference, sometimes its hard to find the right page – 463035818_is_not_an_ai Dec 20 '22 at 08:31
  • 3
    @john: *"`#include` always refers to a source file"*. Standard headers don't have to be actually files, compiler could use another approach as in memory AST or database, or "intrinsics" for those. – Jarod42 Dec 20 '22 at 08:40
  • 1
    *"why don't we write #include?"* Before there was a C++ standard, header files used all kinds of extensions, like .h, .hpp. .hxx, or even .h++. When writing the standard the committee just couldn't agree on which of those to use. The final compromise was to use *no* extension, so ``. – BoP Dec 20 '22 at 10:20
  • @Jarod42 Yes I know that. I'm just trying to keep it simple and comment on the question in the terms that the OP asked it. Personally I have never seen an implementation where they were not files however. No doubt someone will correct me on that. – john Dec 20 '22 at 10:50

2 Answers2

8

iostream and others are header files.

Usually headers have .h or .hpp extension, but it's merely a convention. The C++ standard library uses a different convention, which is to have no extension.


What counts as a library is moot. A "library" can mean either:

  1. A single .a, .so, .lib, or .dll file (or something else, depending on your platform).
  2. A collection of predefined entities for a programmer to use.

The individual standard headers are definitely not (1). The whole standard library is (2), and is usually split into several (1)s.

Whether each individual standard header counts as (2) is moot, I wouldn't call them that.

The C++ standard splits the standard library into several header groups, and calls each a "library" (2). Cppreference does the same.

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
  • In the C++ standard, the notion of library is what you describe as "a collection of predefined entities for a programmer to use". Library files (e.g. `.a`, `.lib`, etc) are particular aspects of implementations, so not specified in the standard (allowing implementations to do what they like). And, incidentally, the standard headers are not required to be files (although they often are in practice, I know of one toolchain that shipped with a precompiled database that the preprocessor would query (e.g. in response to `#include `) rather than a set of separate "header files"). – Peter Dec 20 '22 at 08:39
  • @Peter *"Library files ... are particular aspects of implementations"* Mhm, I wasn't going for a strict language-lawyer answer. *"I know of one toolchain"* Curious, what's that toolchain? – HolyBlackCat Dec 20 '22 at 08:43
  • @HolyBlackCat so actually what we are doing is we are including header files that are there in the compiler? Like we use include cmath then we are including math header file and the actual library for that might be libm.lib or whatever. But cppreference is just calling a bunch of headers as a library which is very different from an actual library as a library contains actual definitions of header files and is not human readable. Am i getting it right?b – Shivam Tanwar Dec 20 '22 at 08:56
  • 1
    @ShivamTanwar *"in the compiler"* - I'd say "distributed with the compiler", but yes. *"as a library contains actual definitions"* Words have different meanings. Cppreference uses (2) here, and you use (1). Both are valid. – HolyBlackCat Dec 20 '22 at 08:59
0

Put it this way: iostream and other #include items are really header files containing libraries of built-in functions and other things in the C++ language. specifically helps you with input and output through the terminal using cin, cout, and more. Also, here is a tip: if you want to avoid the tedious need to add each header file so you can use a variety of functions and other items, just #include <bits/stdc++.h>, which is really just a header that includes every other header and their libraries; It should make your life easier.

Hudson
  • 312
  • 2
  • 18
  • Adding a header that includes a bunch of unnecessary headers may make your life easier while trying to get it to compile but that isn't good practice. – Null Dec 22 '22 at 23:49