I understand that the prototypes of the functions are in the respective header files. The declarations of standard functions are in the standard library and that is why we use the term "using namespace std". But where are the declarations of non-standard functions stored?
-
12_that is why we use the term "using namespace std"_; generally, [we don't](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – Algirdas Preidžius Nov 21 '16 at 14:06
-
Anywhere the implementors want them to be, which is anywhere but the `std` namespace. Usually in the global namespace. – Some programmer dude Nov 21 '16 at 14:07
-
No, experienced C++ developers never use "using namespace std;", as this leads to obscure bugs. As far as non-C++ library functions go, they are declared in the unnamed global namespace. – Sam Varshavchik Nov 21 '16 at 14:07
-
For non-standard functions, as in 'user-defined', they can be stored anywhere, because that's non-standard. – Dean Seo Nov 21 '16 at 14:10
1 Answers
The standard library does not have to be implemented as header files.
The C++ standard dicates what happens when you #include <vector>
. It does not require that vector
be a header file on your system; it could be implemented as a compiler intrinsic that introduces certain symbols and types.
It dictates what happens when you interact with those symbols and types.
It is usually easy to do this as a header file; but there are some C++ features in std
that cannot be implemented in C++. Usually the "surface" interaction is done in C++, but they then fall back on magic compiler intrinsics.
Much of std
can exist and does exist as pure header files. Other parts of it are usually compiled into libraries, often mostly written in C or C++. They interact with operating system libraries, which are also mostly written in C (and sometimes C++ and other languages), which in turn talk to hardware specific code written in a mixture of C and assembly.
The "runtime" library can be dynamically or statically linked to your output, and acts as a kind of "glue" between what C++ requires and what the specific OS provides.
Other libraries can exist. Their header files are stored in a compiler-determined way and searched for in a compiler-determined way. Linking of their libraries, dynamically or statically, is also done in a compiler-determined way, as is where said libraries exist.
They can be written in many languages, so long as they export an interface that matches the ABI that the compiler expects.

- 262,606
- 27
- 330
- 524
-
Could you explain in simpler terms please? I did not quite fully get your answer. Thanks. – Tanmay Bhatnagar Nov 21 '16 at 14:22
-
@TanmayBhatnagar What about how C++ compiles into an executable or library do you understand? – Yakk - Adam Nevraumont Nov 21 '16 at 14:29
-
I understand that when we compile our code, the linked links all the prototypes in the header files to their respective function declarations. Then the compiler converts source code to object code. – Tanmay Bhatnagar Nov 21 '16 at 14:34
-
-
It our code needs some external dependencies the linker links those files to our code and converts it to an executable. – Tanmay Bhatnagar Nov 22 '16 at 18:02
-
@TanmayBhatnagar Ok. So I'm saying that there are libraries where the external dependencies (definitions) of various non-inline `std` functions. The linker finds loads those libraries. – Yakk - Adam Nevraumont Nov 22 '16 at 18:17
-