Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In general, you need both.</p> <p>Include files contain <em>declarations</em> of types, prototypes of functions, <code>inline</code> functions, <code>#define</code>s, ..., in general every information about the library the <em>compiler</em> needs to be aware of when compiling your files.</p> <p>Static libraries, instead, contain the actual object code of the functions of the library. If the headers contain the prototypes, the static libraries contain the (compiled) <em>definitions</em> of the functions, i.e. the object modules that the <em>linker</em> will link with yours.</p> <p>If you only included the header file without linking against the static library, the linker would complain about missing definitions, because you would be using functions <em>declared</em> in the header, but not <em>defined</em> anywhere (=with no implementation). On the other hand, if you only linked the static library without providing the header, the compiler would complain about unknown identifiers, since it wouldn't have a clue about the library symbols you're using.</p> <p>The concept is very similar to when you compile a multi-file project: to access the definitions wrote in other <code>.cpp</code> you need to include just a header with their declarations, and the linker in the end links together the various object modules.</p> <p>As far as dlls are concerned, usually an <em>import library</em> is provided; import libraries are like static libraries, but, instead of containing all the code of the library, they contain small stubs that call the functions into the dll. Every time a call to a library function is encountered in one of your object modules, the linker directs it to the stub, which in turn redirects it to the code into the dll<sup>1</sup>. All in all, when dealing with dlls on Windows you usually have a <code>.h</code> (prototypes/...), a <code>.lib</code> (import library you link against, contains the stubs) and a <code>.dll</code> (dynamic-linking library containing the actual code of the library).</p> <p>By the way, some libraries are "header only" (you can find many in boost), which means that all their code is put into a header, so no static library is needed. Such libraries are often just made of inline code (functions/classes/...) and templates, for which no separate definition is needed.</p> <p>Often this is done because static libraries are ugly beasts for several reasons:</p> <ul> <li>you have to explicitly link against them;</li> <li>since they are linked directly to your code, they have to use exactly your same C/C++ runtime library, which means that, at least on Windows, it's impractical to distribute static libraries (different compilers, different compiler versions, different configurations of the same compiler use different standard libraries, distributing a static library for every combination of these aspects would be impractical at least);</li> <li>because of this, in general you have to <em>first</em> compile on your own version of the static library, and then link against it.</li> </ul> <p>Compare all this with just including a header file... :)</p> <hr> <ol> <li>Actually, modern toolchains can recognize these stubs and avoid the extra indirection step. See <a href="http://blogs.msdn.com/b/oldnewthing/archive/2006/07/27/680250.aspx" rel="noreferrer">this series</a> by Raymond Chen for details.</li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload