Header files contain only the declaration of the function and the actual implementation of the function is in the library. If they don't want to share source code they can share the obj file. Why do we use a Library when the implementation of a function can also be done in another C++ file?
-
1A library ist more or less just a collection of .obj files. For example the standard C library is made of maybe 50 or so .obj files. It's much simpler to distribute one library than 50 .obj files. – Jabberwocky Jul 18 '21 at 07:44
-
1Libraries are a collection of object files. Originally, the library files just were archived object files. Hence, static libraries were build with a call of [ar](https://linux.die.net/man/1/ar). Later, shared objects (`.so`) were introduced which provide advantages like late linking and saving of disk space. – Scheff's Cat Jul 18 '21 at 07:45
-
1Would you rather a) pass a single option per library to your linker or b) pass 300 .o files to the linker. In case you chose option b, how would you feel about having to adjust this linker call every time the library vendor rewrote the code e.g. adding one helper class and removing some classes that are no longer in use. Furthermore shared libraries can be loaded dynamically at runtime, if you require plugin-like functionality; not sure if this would be possible with an object library and even if it war it would be incredible inconventient having to list all the correct .o files. – fabian Jul 18 '21 at 07:52
3 Answers
Usually, a library is a collection of several translation units. A library archive is simply a convenient way to bundle the separate object files into one blob.
Besides that, shared libraries add the ability of dynamic loading and sharing of commonly used libraries between multiple dependents which isn't possible with a plain object file.

- 232,697
- 12
- 197
- 326
Libraries used in general as a collection of functions implements by other developers and can used by another developers " as a general used" , also provide a benefit to minimize your code and also u can take a function from library and edit the implementation as u need.

- 39
- 11
As other people explained I want to add more into it, Library provides you Reusability across multiple systems regardless of in which language they are written in Different Language unless it follows the same ABI. For example, A library written in C can be easily used with Rust, by wrapping the same function in Rust syntax.
For example, a function in C has this signature.
int return_num(int a);
If we write it in Rust, which will interact the same way as C do.
extern "C" {
fn return_num(x: i32) -> i32;
}

- 1,307
- 1
- 9
- 22