Why don’t we develop OpenCV apps only by referencing the OpenCV source code rather than its binaries?

The common workflow explained in most books or sites when developing OpenCV apps is by doing the following:

  1. Optionally build OpenCV binaries from the downloaded OpenCV source code. I say it is optional because for those who work on Windows with Visual Studio, the prebuilt OpenCV binaries are included officially by OpenCV so they don’t need to do this step.
  2. If the OpenCV binaries are available, we develop OpenCV apps by creating our own code referencing the binaries.

Questions

The above workflow seems to make the file size of apps relatively bigger because we have to include OpenCV DLLs that contain many many code that we may not use in the apps.

Why don’t we develop our apps by referencing the OpenCV source code directly so we can minimizing the file size of our apps?

Answer

OpenCV is quite modular, and you don’t have to link with DLLs that you don’t use. For example, if you don’t use the videoio module, you don’t need to link with it and don’t need to ship the corresponding DLL.

And if that isn’t enough of a size reduction, you can link OpenCV statically, which allows the linker to remove unused code.

Adding the OpenCV source code to your own project gives the same effect, but you would have to replicate everything that the OpenCV build system does for you, such as finding dependencies and setting compiler flags. This is a huge hassle.

Moreover, on platforms like Linux, it’s common to use a package manager to install OpenCV binaries in a central location only once, and all applications can depend on them being installed. This leads to the smallest possible applications on those platforms.