2

i want to know that in a c++ code during execution how iostream file is founded. we write #include in c++ program and i know about #include which is a preprocessor directive to load files and is a file name but i don't know that how that file is located. i have some questions in my mind...

  1. Is Standard library present in compiler which we are using?
  2. Is that file is present in standard library or in our computer?
  3. Can we give directory path to locate the file through c++ code if yes then how?
Sam
  • 7,252
  • 16
  • 46
  • 65

4 Answers4

4

You seem to be confused about the compilation and execution model of C++. C++ is generally not interpreted (though it could) but instead a native binary is produced during the compilation phase, which is then executed... So let's take a detour.


In order to go from a handful of text files to a program being executed, there are several steps:

  • compilation
  • link
  • load
  • execution proper

I will only describe what traditional compilers do (such as gcc or clang), potential variations will be indicated later on.

Compiling

During compilation, each source file (generally .cpp or .cxx though the compiler could care less) is processed to produced an object file (generally .o on Linux):

  1. The source file is preprocessed: this means resolving the #include (copy/pasting the included file in the current file), navigating the #if and #else to remove unneeded sources and expanding macros.
  2. The preprocessed file is fed to the compiler which will produce native code for each function, static or global variable, etc... the format depends on the target system in general
  3. The compiler outputs the object file (it's a binary format, in general)

Linking

In this phase, multiple object files are assembled together into a library or an executable. In the case of a static library or a statically-linked executable, the libraries it depend on are also assembled in the produced file.

Traditionally, the linker job is relatively simple: it just concatenates all object files, which already contain the binary format the target machine can execute. However it often actually does more: in C and C++ inline functions are duplicated across object files, so the linker need to keep only one of the definitions, for example.

At this point, the program is "compiled", and we live the realm of the compiler.

Loading

When you ask to execute a program, the OS will load its code into memory (thanks to a loader) and execute it.

In the case of a statically linked executable, this is easy: it's just a single big blob of code that need be loaded. In the case of a dynamically linked executable, it implies finding the dependencies and "resolving the symbols", I'll describe this below:

  • First of all, your dynamically linked executable and libraries have a section describing which other libraries they depend on. They only have the name of the library, not its exact location, so the loader will search among a list of paths (LD_LIBRARY_PATH for example on Linux) for the libraries and actually load them.
  • When loading a library, the loader will perform replacements. Your executable had placeholders saying "Here should be the address of the printf function", and the loader will replace that placeholder with the actual address.

Once everything is loaded properly, all symbols should be resolved. If some symbols are missing, ie the code for them is not found in either the present library or any of its dependencies, then you generally get an error (either immediately, or only when the symbol is actually needed if you use lazy loading).

Executing

The code (assembler instruction in binary format) is now executed. In C++, this starts with building the global and static objects (at file scope, not function-static), and then goes on to calling your main function.

Caveats: this is a simplified view, nowadays Link-Time Optimizations mean that the linker will do more and more, the loader could actually perform optimizations too and as mentioned, using lazy loading, the loader might be invoked after the execution started... still, you've got to start somewhere, don't you ?


So, what does it mean about your question ?

The #include <iostream> in your source code is a pre-processor directive. It is thus fully resolved early in the compilation phase, and only depends on finding the appropriate header (no library code is actually needed yet). Note: a compiler would be allowed not to have a header file sitting around, and just magically inject the necessary code as if the header file existed, because this is a Standard Library header (thus special). For regular headers (yours) the pre-processor is invoked.

Then, at link-time:

  • if you use static linking, the linker will search for the Standard Library and include it in the executable it produces
  • if you use dynamic linking, the linker will note that it depends on the Standard Library file (libc++.so for example) and that the produced code is missing an implementation of printf (for example)

Then, at load time:

  • if you used dynamic linking, the loader will search for the Standard Library and load its code

Then, at execution time, the code (yours and its dependencies) is finally executed.


Several Standard Library implementations exist, off the top of my head:

  • MSVC ships with a modified version of Dirkumware
  • gcc ships with libstdc++ (which depends on libc)
  • clang ships with libc++ (which depends on libc), but may use libstdc++ instead (with compiler flags)

And of course, you could provide others... probably... though setting it up might not be easy.

Which is ultimately used depends on the compiler options you use. By default the most common compiler ship with their own implementation and use it without any intervention on your part.


And finally yes, you can indeed specify paths in a #include directive. For example, using boost:

#include <boost/optional.hpp>
#include <boost/algorithm/string/trim.hpp>

you can even use relative paths:

#include <../myotherproject/x.hpp>

though this is considered poor form by some (since it breaks as soon as your reorganize your files).

What matters is that the pre-processor will look through a list of directories, and for each directory append / and the path you specified. If this creates the path of an existing file, it picks it, otherwise it continues to the next directory... until it runs out (and complain).

Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
0

The <iostream> file is just not needed during execution. But that's just the header. You do need the standard library, but that's generally named differently if not included outright into your executable.

The C++ Standard Library doesn't ship with your OS, although on many Linux systems the line between OS and common libraries such as the C++ Standard Library is a bit thin.

How you locate that library is very much OS dependent.

MSalters
  • 173,980
  • 10
  • 155
  • 350
0

There can be 2 ways to load a header file (like iostream.h) in C++

if you write the code as:

# include <iostream>

It will look up the header file in include directory of your C++ compiler and will load it

Other way you can give the full path of the header file as:

# include "path_of_file.h"

And loading up the file is OS dependent as answered by MSalters

Community
  • 1
  • 1
Arpit
  • 21
  • 1
  • 2
  • The `` part is technically wrong (too definite, assumes one implementation). There might or might not be a header file `iostream.h`. The Standard just says how `#include ` should behave, and doesn't require at all that the standard headers are implemented as separate files from an include directory. they could be precompiled, built-in things instead. – MSalters Jun 03 '14 at 08:11
0
  1. You definitely required the standard library header files so that pre-processor directive can locate them.
  2. Yes those files are present in the library and on include copied to our code.

  3. if we had defined the our own header file then we have to give path of that file. In that way we can include also *.c or *.cpp along with the header files in which we had defined various methods and those had to include at pre-processing time.

Community
  • 1
  • 1
suneet saini
  • 592
  • 3
  • 16