How to build opencv & opencv_contrib on Ubuntu desktop

I’m trying to build OpenCV 4.5.2 on Ubuntu 20.4 with CUDA 11.2, architecture Turing. It seems building OpenCV with video IO requires OpenCL and that is giving me problems. For example trying to build opencv_samples_cudaoptflow builds and links opencv_cudaarithm, opencv_imgproc, etc. But it fails to link libopencv_videoio with a relocation error. Indeed, cmake warned about the opencl runtime dependencies “runtime library [] in /usr/lib/x86_64-linux-gnu may be hidden by files in: /usr/lib/cuda/lib64”. I’m not sure how I got in my cuda installation. Possibly this is from NVEncode/Decode. I’ve tried setting CMake variable BUILD_opencv_cudacodec=OFF to no avail.
Any advice?

This was fixed by setting CMake variable OPENCL_LIBRARY equal to CUDA_OpenCL_LIBRARY. In my case it also required installing HDF5 as a static library