2

When you build an executable in C++, when including header files

For example, #include <iostream>

Does the Preprocessor find the library iostream library and the function used or is the object code for the library functions injected at Linking?

Streamline Astra
  • 307
  • 2
  • 11
  • 3
    `#include `basically pastes the content of the header into your source during preprocessing. libs and object files are handled by the linker. – 3Dave Apr 02 '20 at 17:58
  • Headers basically tell the compiler "Here are some declarations. I promise this stuff is actually defined in another object module." The linker then attempts to fixup those references when linking objs and static libs together. If it can't find one, it'll generate an "unresolved external" error. – 3Dave Apr 02 '20 at 18:00
  • 1
    This is a really good question. My only issue is I'd be really surprised if it hasn't been asked before. –  Apr 02 '20 at 18:02
  • Gopt it thank you so much, alwyas heard from teachers 'include pastes the content code required' then when i encounter Dynamica and static linking i thought it could be the definition, thanks for cleaqring it up – Streamline Astra Apr 02 '20 at 18:02
  • Actually it might be both. Some libraries are header only and don't require linking ;) – Lukas-T Apr 02 '20 at 18:05
  • If this is a discussion of Visual Studio note that a header file can add linker settings to the code that includes a header. Google for: `#pragma comment(lib` – drescherjm Apr 02 '20 at 18:06
  • 1
    Does this answer your question? [How does #include work in C++?](https://stackoverflow.com/questions/35720656/how-does-include-work-in-c) –  Apr 02 '20 at 18:14

1 Answers1

5

All #include does is load in the definitions of the library functions and doesn't have anything to do with the binary instance of library itself.

Think of the header files as blueprints on how the library works, but does not provide the actual components that the program needs. It's just so the compiler can understand how the library works.

In order for that to come into play you must also link in the associated library files. This is done with different arguments at the linking stage.

tadman
  • 208,517
  • 23
  • 234
  • 262
  • Sorry do you mean load as in loading in memory? Or load meaning just pasting the definitions into a Cpp translation unit? Do you mean that header files are considered high level representations of library functions, classes but their definitions are not provided? – Streamline Astra Apr 02 '20 at 18:13
  • 1
    The compiler needs blueprints of the functions it's working with: What arguments do they take? What return values do they produce? What conditions apply to usage? The actual implementation is not necessarily provided, that may be located in a binary library. It loads in these *definitions* and uses them to verify the functions are used correctly, as well as to select the correct function for the task based on what's in your compiled code. Later the linker adds in the implementation library to your compiled target. – tadman Apr 02 '20 at 18:15
  • 1
    Header files are often just a high-level representation, but in some cases, as with template functions or inline functions, they're the whole implementation as well. It depends on the type of function you're talking about. – tadman Apr 02 '20 at 18:16
  • 1
    @StreamlineAstra *Or load meaning just pasting the definitions into a Cpp translation unit?* Pretty much. After the preprocessor's finished you wind up with one big file that is the source file, all of the `#include`ed files, all substitutions from macros performed, and any implementation defined extras such as `#pragma` that are resolved during preprocessing.Whether that file's actually written anywhere or just living in memory is up to the implementation and compiler options used. – user4581301 Apr 02 '20 at 18:39