-1

My C++ desktop app is an exe which is linked to a dozen static libraries (.lib files) and uses MFC in a static library. The total size of a debug build of the exe is 25 Mb.

I'm adding a new feature to it and I have to choose between creating a new static library or adding my feature in one of the existing static library.

What are the tradeoffs? Since our number of static libs is relatively small and since the outputted exe is also small, what are the disadvantages to simply merge all our small libraries into a big one?

Brainless
  • 1,522
  • 1
  • 16
  • 30

1 Answers1

1

Before answering your question, I think it is important to remember the question relating to the choice of lib and dll.

  1. In the case of a .lib

    • The size of your binaries increases according to the size of your libraries, regardless of whether your libraries will be used or not at run-time.
  2. In the case of a .dll

    • .dll are loaded as needed (during the first call) and can be shared between the different components throughout the life cycle of your program.

    • They allow better flexibility because they can be versioned and managed in a more modular way

    • You can manage the compatibility problems better, especially specially during the update.

In your case:

  • By choosing a solution based on .lib you inherit all the disadvantages of .lib ...
  • By grouping the different libs into one, you kill the modularity of your solution:
    • Imagine you merge A.lib and B.lib in C.lib If tomorrow you will only need just B.lib functionality, you will have to link with C.lib and your binaries will increase considerably even though you need only B.lib
  • Today your binaries are small, but you don't know what's will happen tomorrow ...

I think it is necessary to review the architecture of the librairies taking into account these aspects as long as things are not complicated.
In your place, I will opt for a dll-based solution, by promoting the modulation of my libraries.

Landstalker
  • 1,368
  • 8
  • 9
  • If you link against the import library of a DLL (using the .lib file), that DLL is loaded at *load* time, not on demand. Unless you are using the special `/DELAYLOAD` linker option. You've also missed to call out [Potential Errors Passing CRT Objects Across DLL Boundaries](https://learn.microsoft.com/en-us/cpp/c-runtime-library/potential-errors-passing-crt-objects-across-dll-boundaries). That is relevant, and particularly cumbersome with MFC. Also MFC, has additional requirements when placing code in DLLs. This answer is misleading, at best. – IInspectable Apr 02 '20 at 11:56
  • @IInspectable This answer was not intended to present a full course on dlls. it was just to present new ideas – Landstalker Apr 02 '20 at 12:04
  • A question that's not answerable should not be answered, but closed. Regardless, ignoring the peculiarities of MFC in an answer to a question tagged [tag:mfc] is almost criminally negligent. – IInspectable Apr 02 '20 at 12:10
  • Also, *"The size of your binaries increases according to the size of your libraries, regardless of whether your libraries will be used or not at run-time"* is wrong. The linker links units (.obj files), and a library may contain multiple units. These are included in the executable only if you actually use one of the items they contain, eg if you call a function. Ie an additional library won't increase the size of the executable, unless you really use it. You may want to split the libraries' source into multiple source files, to decrease the executable file if this is deemed at all important. – Constantine Georgiou Apr 02 '20 at 12:14
  • Lord, 2 upvotes to an answer that's not even technically correct. This place is so lost. Let me repeat: By default, DLLs aren't loaded *"on demand"*, but at process **load** time. – IInspectable Apr 02 '20 at 12:14
  • @con: What's more, link-time code generation can (drastically) reduce the size and improve performance when using static libraries. Something that's not ever possible with dynamically linked libraries. – IInspectable Apr 02 '20 at 12:16
  • Have a good reading : https://stackoverflow.com/questions/140061/when-to-use-dynamic-vs-static-libraries – Landstalker Apr 02 '20 at 12:21
  • That answer was posted more than a decade ago. Surely you can acknowledge, that things have changed since. Regardless of how hard you try, this answer is misleading, technically incorrect on more than one account, and fails to address very real issues pertaining to MFC. – IInspectable Apr 02 '20 at 12:59