C++ : Difference between linking library and adding include directories

19,618

Solution 1

In general, you need both.

Include files contain declarations of types, prototypes of functions, inline functions, #defines, ..., in general every information about the library the compiler needs to be aware of when compiling your files.

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) definitions of the functions, i.e. the object modules that the linker will link with yours.

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 declared in the header, but not defined anywhere (i.e. 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.

The concept is very similar to when you compile a multi-file project: to access the definitions written in other .cpp you need to include just a header with their declarations, and the linker in the end links together the various object modules.

As far as dlls are concerned, usually an import library 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 dll1. All in all, when dealing with dlls on Windows you usually have a .h (prototypes/...), a .lib (import library you link against, contains the stubs) and a .dll (dynamic-linking library containing the actual code of the library).

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.

Often this is done because static libraries are ugly beasts for several reasons:

  • you have to explicitly link against them;
  • 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);
  • because of this, in general you have to first compile on your own version of the static library, and only then link against it.

Compare all this with just including a header file... :)


  1. Actually, modern toolchains can recognize these stubs and avoid the extra indirection step. See this series by Raymond Chen for details.

Solution 2

The compiler needs to know the include directories, since it needs to include header (interface) files of libraries you want to use.

The linker needs to know the library directories, since it needs to link your executable to the (precompiled) implementation of the library.

See also What are the differences between a compiler and a linker?

Solution 3

Include directories are just for header files, which typically provide function/method signatures only. You need to link to a library to have access to its actual object code.

See this question.

Share:
19,618

Related videos on Youtube

Setheron
Author by

Setheron

Software Engineer Graduate at the University of Waterloo. Having completed plenty of internships through my program at UW, I'm hoping to make a nice splash in the software industry.

Updated on June 02, 2022

Comments

  • Setheron
    Setheron almost 2 years

    Pretty much title sums it up.

    I'm not sure the difference between the two if i'd like to use a library.

    Thanks!

    • Petr Abdulin
      Petr Abdulin almost 13 years
      Please, clarify your question with example.
    • Oliver Charlesworth
      Oliver Charlesworth almost 13 years
      This is an "apples and oranges" comparison. They're completely unrelated things. Please add some context here.
    • SSH This
      SSH This over 11 years
      I had a similar confusion about this, so I think it's relevent. For complete newbies, this is good information, especially when it is answered so thoroughly by @Matteo Italia.
    • Basile Starynkevitch
      Basile Starynkevitch about 9 years
  • Setheron
    Setheron almost 13 years
    so DLLs are given as the .dll and header files always?
  • Jaison Varghese
    Jaison Varghese over 11 years
    Wonderful, Detailed explanation. Impressive :)
  • html_programmer
    html_programmer over 8 years
    That's what they call abstraction.
  • KANJICODER
    KANJICODER almost 7 years
    Sometimes I confuse the two, so I have a saying "Linker Likes Libraries" If anyone has a mnemoic for compiler, I'd like to hear.
  • gonidelis
    gonidelis about 3 years
    What's the relationship between a static library and a "header only" library? Is one a subset of the other?
  • Matteo Italia
    Matteo Italia about 3 years
    @giannisgonidelis a header-only library is a library where the whole implementation is in header files (it's generally done with inline functions), possibly just one; so, no .cpp nor the corresponding object files are needed. Indeed they are often appreciated because they don't add any build complexity - as long as you can make the compiler see the relevant includes you are set.