3

I'm currently developing a shared library on Windows (dll) with c++. The library depends on another external library.

What is the best way to link them together?

  • Link the external library as a static library into my shared lib?
  • Link it as a shared library and provide the dll to the application who is using my shared lib?

For the second case what happens if i create an application which uses my own created shared library and also the external library as a shared library?

If for example my shared library is build with the external library version 1.1 and the application uses the newer Version for example 1.3 ? Now the dlls should be different but how could i provide them to the main application?

Are there some best practices or recommendations on how to handle such a situation?

Ardent Coder
  • 3,777
  • 9
  • 27
  • 53
Kevin
  • 785
  • 2
  • 10
  • 32

1 Answers1

1

This really depends on what you want to do and how you are deploying your library.

shared/dynamic libraries (dll's on windows) have several advantages over static libraries

  • They can be distributed externally from the application, allowing your application binary to be smaller
  • They can be updated externally to the application
  • They are slightly more efficient as code is only executed if its needed rather than being bundled with the executable

Of course they have several weaknesses too

  • They can be distributed externally from your application and updated externally - allowing dll injection attacks
  • Trying to release dynamic libraries with the executable is one of the most painful and horrible things to do (especially on windows where there is no RPATH)

You may have to use a dll, dependent on your external libraries licensing, Qt for example requires shared library linking in many cases (not all).

Standard convention is usually to offer shared and static versions of your library with the shared version being completely linked to other shared libraries and the static version being an ar static library (includes all dependencies). The shared library variant then offers instructions on linking (i.e a .pc (pkgconfig) file) which specifies the versions of the other libraries to link to (i.e v1.1 of x.dll) in order to successfully compile/link.

Object object
  • 1,939
  • 10
  • 19