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

8
  • Those header files is just no extension, stackoverflow.com/questions/441568/… Commented Dec 20, 2022 at 8:16
  • <iostream> 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 #includes other standard headers related to I/O (e.g. <ios>). 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). <iostream.h> has never been part of standard C++, but was a precursor to <iostream> that predated the first C++ standard (which was ratified in 1989). Commented Dec 20, 2022 at 8: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. Commented Dec 20, 2022 at 8:25
  • 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. Commented Dec 20, 2022 at 8:40
  • 1
    "why don't we write #include<vector.h>?" 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 <vector>. Commented Dec 20, 2022 at 10:20

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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 <iostream>) rather than a set of separate "header files").
@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 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
@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.
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.

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.