0

Specifically I'm confused about a line in an answer for another question (here: What does the linker do?)

The #include pre-processor directive does not copy the code from libraries to your source code. It deals with header files (i.e., the ones that end with the .h extension).

And does #include paste in the whole header file or just the relevant information? And does linking the library paste the whole library (all those function definitions) or just the relevant information (the definitions of the functions you used)?

0
2

Developers often split libraries into two kinds of files:

  1. header files
  2. implementation files

A header file defines an interface of a library — which functions, global variables, etc are available to use by the library clients (i.e., code that uses tools from the library).

Header files typically end with a .h extension (e.g., stdio.h and cs50.h) and are included into your program with the #include preprocessor directive.

Implementation files contain the implementation details (e.g., function definitions).

Both header files and implementation files are compiled together and produce the library's object file (the one that contains the 0s and 1s of the library).

Lastly, when you link a library's object file to your program, the object code of the library gets linked with the object code of your program to produce the final executable file via which you execute your program.

Watch this for more info!

And does #include paste in the whole header file or just the relevant information?

The whole file. Compile your program with the -E option to see the output of the preprocessor.

does linking the library paste the whole library (all those function definitions) or just the relevant information (the definitions of the functions you used)?

All I can say at the mean time is that the linking process is a bit more complicated than that. When the compiler doesn't find a definition of referenced function, it just assumes that it is defined in another file leaving that for the linker to resolve. So you may think of the linker as a program that resolves these undefined references by linking them to their already compiled definitions.

Frankly, I have no detailed info whether the whole object code of the library is used in this process or just the code for the referenced functions (hopefully I'll update this answer once I learn more) but the knowledge that you should get from this answer should probably be enough for you to proceed for now.

0

Found this short on libraries: https://www.youtube.com/watch?v=ED7QtgXDShY&feature=youtu.be

0

A header file is generally used to define an interface or set of interfaces within an application. Think of a header file as something which shows the external functionality of a program while omitting the technical implementation details.

For example, if you were optimizing a program, you would most likely modify the source (.cpp) file to improve the algorithm, but the header file wouldn't change, because external clients still call the methods using the same set of parameters and return values.

In an object-oriented language like C++, a header file generally includes the following:

  • Class description and inheritance hierarchy

  • Class data members and types

  • Class methods

While there is nothing stopping code from being implemented in a header file, this is generally not favored as it can introduce extra coupling and dependencies in the code.

In some cases (e.g. templated classes) the implementation must be defined in the header file for technical reasons.

A library is a collection of code which you want to make available to a program or group of programs. It includes the implementation of a particular interface or set of interfaces.

Code is defined in a library to prevent code duplication and encourage re-use. A library can be statically-linked (.lib) or dynamically-linked (.dll):

  • A statically-linked library defines a set of export symbols (which can be thought of as method definitions) which are then linked into the final executable (.exe) during the linking stage of the build process. It has the advantage of faster execution time (as the library doesn't need to be dynamically loaded), at the expense of a larger binary (because the methods are essentially replicated in the executable file).

  • A dynamically-linked library is linked during the execution of a program, rather than the linking of a program. It is useful when multiple programs need to re-use the same methods, and is used extensively in technologies such as COM.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .