Library?

What is mean by header and library in C++??
"Header" refers to a header file. This is a file containing common declarations that will be used by the code in other files. For example the file "string" contains the definition of the std::string class. You use a header file by #including it in your code file. For example, #include <string> The #included file behaves as if its contents were copied and pasted into your file.

A library is a file containing pre-compiled code that programs can use. Usually a library comes with a header a file. So you #include the header file to get the declarations of the functions and classes. Then you "link" with the library to add the library code to your program.

Some differences:
- because a header file acts like it was copied and pasted into your file, it shouldn't define any code that isn't inline, or any data. Otherwise of you #included it into two files, you'd get multiply defined code/data. It's okay to declare stuff, you just can't define it.

- The linking system doesn't include the entire library in your program, only the parts that your program actually needs. So if the library defines really_big_function() and your program doesn't call it, then it might not end up in your program. I say "might not" because the level of granlarity is actually a compilation unit (a C++ file). So if your program needs small_function() and really_big_function() was in the same c++ file when they created the library, then you'll get really_big_function() too.

- Libraries can be dynamically linked. These are called shared or dynamic link libraries (DLLs in windows). They are combined with the program at run-time. A really big advantage of DLLs is that several programs can share one copy of the code at run time. So for example if your computer is running 50 processes that use the same library (like windows procs using the C library), they share just one copy of the code.

Hope this helps.
Topic archived. No new replies allowed.