How to select/reduce the used OpenCV libraries linked by CMake project?

I compiled OpenCV 4.6 to use it in my projects.

In my project I use a CMakeLists.txt with

find_package( OpenCV 4.6 REQUIRED )

to generate a visual studio 2019 project.
Which works. I can open, compile and link my project and run the application.
However the project links all the available 56 opencv modules to my project, even if I would only need opencv_core.

In CMake I add the libraries in this way to my project:



where the OpenCV_LIBS variable was assigned by the find_package call.

How can I select the actually wanted/used libs?

i think, it can be handled at 2 levels:

  1. opencv library: build only, what you want:
    there’s a cmake “whitelist” option (overriding all BUILD_opencv_xxx):
    cmake -DBUILD_LIST="core,imgproc" // comma seperated list

  2. your project: only depend on what you need:


    this is indeed the list of all available modules
    replace with an explicit list like opencv_core opencv_imgproc opencv_imgcodecs opencv_highgui

    (as a rule of thumb: whatever opencv header you #include will need its counterpart in the libs)