1

I have read this where it is said that the header is for the interface and the library is implementation of one particular or more interfaces.. But isn't the source files also for the implementation of the interface? To me, the description of libraries(the implementation of the interfaces) is the exact same as for source files.. Then, what is the difference? :/

That Guy
  • 2,349
  • 2
  • 12
  • 18
  • 1
    Library == compiled source files. Source files == .. well, source code. – Jesper Juhl Sep 06 '17 at 18:13
  • @JesperJuhl okay, so libraries consist of one or multiple object files? Isn't every source code when it is compiled then a library because it is an object file? – That Guy Sep 06 '17 at 18:20
  • @overso No, a single object file isn't considered a _library_. It's just like the term suggests. Did you ever enter a library that stored a single book? – user0042 Sep 06 '17 at 18:24
  • @user0042 Yea.. I have not.. But since a library can be defined as the implementation for _one_ or more interfaces, doesn't it mean _one_ or more object file(compiled source code) – That Guy Sep 06 '17 at 18:25
  • 1
    @over _One or more_, yes. There may be even poor libraries that store a single book (theoretically). – user0042 Sep 06 '17 at 18:28
  • @user0042 But that means that an object file can essentially be the same as a library(technically)? – That Guy Sep 06 '17 at 18:34
  • 1
    A library can consist f a single object file. –  Sep 06 '17 at 18:36
  • @user0042 I mean, if this is true, how can we then differentiate between them? – That Guy Sep 06 '17 at 18:36
  • @NeilButterworth which means that a library and an object can technically be the exact same thing? – That Guy Sep 06 '17 at 18:36
  • @over What differentiation do you need? I don't get your concern? – user0042 Sep 06 '17 at 18:38
  • @over No, a library has some extra features, such as an index to make it easily searchable. A library (in the sense of a .lib or .a file) is a concatenation of one or more object files, plus some extra stuff. –  Sep 06 '17 at 18:51
  • @user0042 sometimes I just wonder why things aren't called the same if the are the same.. But appearently headers can do something more.. but is based on object files.. – That Guy Sep 06 '17 at 19:12
  • @NeilButterworth ahh okay, but is it possible to include object files directly in your code as you can with libraries? – That Guy Sep 06 '17 at 19:12
  • You cannot include libraries directly in your code - you are probably thinking of header files. The only thing that understands libraries or object files is the linker. –  Sep 06 '17 at 19:15
  • @NeilButterworth ahh okay, thanks buddy. But if you cannot include libs and object files directly into the code but only headers (and source files), will headers for libraries then work just as when you include a header that is associated with its corresponding source file(its implementation).. Because if it is so.. then it only leads to more similarities.. – That Guy Sep 06 '17 at 19:37
  • @NeilButterworth like does #include provide access to the functionality in a given library just as #include "someclass.h" would provide access/knowledge to the functionality of a given associated source file? – That Guy Sep 06 '17 at 19:40
  • It tells your code what the interface to the library is, but it isn't generally the library. Having said that, there are header-only libraries where all the code is in the header file, and there is no .lib or .o file. –  Sep 06 '17 at 19:47

1 Answers1

1

Then, what is the difference?

A source file will be compiled to a single object file that implements the interface declared in a corresponding header.

A library usually consists of the object files build from more than a single source file.
That's the only difference more or less.


I'll try to put a better methaphor than the accepted answer from your link:

  • A header file is like a books table of contents
  • A source file fleshes out all the chapters of that book
  • A library stores many books
user0042
  • 7,917
  • 3
  • 24
  • 39