Opencv Ubuntu 22.04 build from source with cuda support

I am trying to build opencv with cuda support. I make using the following cmake options:

cmake
-DOPENCV_EXTRA_MODULES_PATH=/mnt/microbbeloth/projects/opencv_contrib/modules
-DCMAKE_INSTALL_PREFIX=/usr/local
-DCMAKE_BUILD_TYPE=RELEASE
-DCUDNN_LIBRARY=/usr/lib/x86_64-linux-gnu/lib
-DCUDNN_INCLUDE_DIR=/usr/include/x86_64-linux-gnu
-DPYTHON_EXECUTABLE=/usr/bin/python3
-DWITH_V4L=ON
-DWITH_OPENGL=ON
-DWITH_GSTREAMER=ON
-DBUILD_DOCS=ON ../

Everything looks okay from the cmake build output with the exception of the C/C++ section which shows an extra dependency of lib. Given a later attempt to build the code results in -llib not being found when working on the libcudev portion, I’m sure something isnt’ configured correctly. The full output from cmake is:

microbbeloth@RobbelothResearch:/mnt/microbbeloth/projects/opencv/build$ cmake \
-DOPENCV_EXTRA_MODULES_PATH=/mnt/microbbeloth/projects/opencv_contrib/modules \
-DCMAKE_INSTALL_PREFIX=/usr/local \
-DCMAKE_BUILD_TYPE=RELEASE \
-DCUDNN_LIBRARY=/usr/lib/x86_64-linux-gnu/lib \
-DCUDNN_INCLUDE_DIR=/usr/include/x86_64-linux-gnu \
-DPYTHON_EXECUTABLE=/usr/bin/python3 \
-DWITH_V4L=ON \
-DWITH_OPENGL=ON \
-DWITH_GSTREAMER=ON \
-DBUILD_DOCS=ON ../
-- Detected processor: x86_64
-- Looking for ccache - found (/usr/bin/ccache)
-- Found ZLIB: /usr/lib/x86_64-linux-gnu/libz.so (found suitable version "1.2.11", minimum required is "1.2.3") 
-- Found system OpenJPEG: openjp2 (found version "")
-- Found ZLIB: /usr/lib/x86_64-linux-gnu/libz.so (found version "1.2.11") 
-- Found OpenEXR: /usr/lib/x86_64-linux-gnu/libIlmImf-2_5.so
-- found Intel IPP (ICV version): 2020.0.0 [2020.0.0 Gold]
-- at: /mnt/microbbeloth/projects/opencv/build/3rdparty/ippicv/ippicv_lnx/icv
-- found Intel IPP Integration Wrappers sources: 2020.0.0
-- at: /mnt/microbbeloth/projects/opencv/build/3rdparty/ippicv/ippicv_lnx/iw
-- CUDA detected: 11.5
-- CUDA: Using CUDA_ARCH_BIN=3.5;3.7;5.0;5.2;6.0;6.1;7.0;7.5;8.0;8.6
-- CUDA NVCC target flags: -gencode;arch=compute_35,code=sm_35;-gencode;arch=compute_37,code=sm_37;-gencode;arch=compute_50,code=sm_50;-gencode;arch=compute_52,code=sm_52;-gencode;arch=compute_60,code=sm_60;-gencode;arch=compute_61,code=sm_61;-gencode;arch=compute_70,code=sm_70;-gencode;arch=compute_75,code=sm_75;-gencode;arch=compute_80,code=sm_80;-gencode;arch=compute_86,code=sm_86;-D_FORCE_INLINES
-- LAPACK(Atlas): LAPACK_LIBRARIES: /usr/lib/x86_64-linux-gnu/liblapack.so;/usr/lib/x86_64-linux-gnu/libcblas.so;/usr/lib/x86_64-linux-gnu/libatlas.so
-- LAPACK(Atlas): Support is enabled.
-- Found apache ant: /bin/ant (1.10.12)
-- Found VTK 9.1.0
-- Checking for module 'libavresample'
--   No package 'libavresample' found
-- freetype2:   YES (ver 24.1.18)
-- harfbuzz:    YES (ver 2.7.4)
-- Julia not found. Not compiling Julia Bindings. 
-- No preference for use of exported gflags CMake configuration set, and no hints for include/library directories provided. Defaulting to preferring an installed/exported gflags CMake configuration if available.
-- Failed to find installed gflags CMake configuration, searching for gflags build directories exported with CMake.
-- Failed to find gflags - Failed to find an installed/exported CMake configuration for gflags, will perform search for installed gflags components.
-- Failed to find gflags - Could not find gflags include directory, set GFLAGS_INCLUDE_DIR to directory containing gflags/gflags.h
-- Failed to find glog - Could not find glog include directory, set GLOG_INCLUDE_DIR to directory containing glog/logging.h
-- Module opencv_sfm disabled because the following dependencies are not found: Glog/Gflags
-- Tesseract:   YES (ver 4.1.1)
-- Allocator metrics storage type: 'long long'
-- Registering hook 'INIT_MODULE_SOURCES_opencv_dnn': /mnt/microbbeloth/projects/opencv/modules/dnn/cmake/hooks/INIT_MODULE_SOURCES_opencv_dnn.cmake
-- Excluding from source files list: <BUILD>/modules/dnn/layers/layers_common.rvv.cpp
-- highgui: using builtin backend: GTK3
-- Building with NVIDIA Optical Flow API 2.0
-- Found 'misc' Python modules from /mnt/microbbeloth/projects/opencv/modules/python/package/extra_modules
-- Found 'mat_wrapper;utils' Python modules from /mnt/microbbeloth/projects/opencv/modules/core/misc/python/package
-- Found 'gapi' Python modules from /mnt/microbbeloth/projects/opencv/modules/gapi/misc/python/package
-- 
-- General configuration for OpenCV 4.6.0-pre =====================================
--   Version control:               4.5.5-432-g8ca394efaf
-- 
--   Extra modules:
--     Location (extra):            /mnt/microbbeloth/projects/opencv_contrib/modules
--     Version control (extra):     4.5.5-86-g84f8ea8e
-- 
--   Platform:
--     Timestamp:                   2022-05-27T18:23:55Z
--     Host:                        Linux 5.15.0-33-generic x86_64
--     CMake:                       3.22.1
--     CMake generator:             Unix Makefiles
--     CMake build tool:            /usr/bin/gmake
--     Configuration:               RELEASE
-- 
--   CPU/HW features:
--     Baseline:                    SSE SSE2 SSE3
--       requested:                 SSE3
--     Dispatched code generation:  SSE4_1 SSE4_2 FP16 AVX AVX2 AVX512_SKX
--       requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
--       SSE4_1 (18 files):         + SSSE3 SSE4_1
--       SSE4_2 (2 files):          + SSSE3 SSE4_1 POPCNT SSE4_2
--       FP16 (1 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
--       AVX (5 files):             + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
--       AVX2 (33 files):           + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
--       AVX512_SKX (8 files):      + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_COMMON AVX512_SKX
-- 
--   C/C++:
--     Built as dynamic libs?:      YES
--     C++ standard:                11
--     C++ Compiler:                /usr/bin/c++  (ver 11.2.0)
--     C++ flags (Release):         -fsigned-char -ffast-math -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden   -DNDEBUG
--     C++ flags (Debug):           -fsigned-char -ffast-math -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden   -O0 -DDEBUG -D_DEBUG
--     C Compiler:                  /usr/bin/cc
--     C flags (Release):           -fsigned-char -ffast-math -W -Wall -Wreturn-type -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -O3 -DNDEBUG  -DNDEBUG
--     C flags (Debug):             -fsigned-char -ffast-math -W -Wall -Wreturn-type -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -g  -O0 -DDEBUG -D_DEBUG
--     Linker flags (Release):      -Wl,--exclude-libs,libippicv.a -Wl,--exclude-libs,libippiw.a   -Wl,--gc-sections -Wl,--as-needed -Wl,--no-undefined  
--     Linker flags (Debug):        -Wl,--exclude-libs,libippicv.a -Wl,--exclude-libs,libippiw.a   -Wl,--gc-sections -Wl,--as-needed -Wl,--no-undefined  
--     ccache:                      YES
--     Precompiled headers:         NO
--     Extra dependencies:          dl m pthread rt cudart nppc nppial nppicc nppidei nppif nppig nppim nppist nppisu nppitc npps cublas lib cufft -L/usr/lib/x86_64-linux-gnu
--     3rdparty dependencies:
-- 
--   OpenCV modules:
--     To be built:                 alphamat aruco barcode bgsegm bioinspired calib3d ccalib core cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev datasets dnn dnn_objdetect dnn_superres dpm face features2d flann freetype fuzzy gapi hdf hfs highgui img_hash imgcodecs imgproc intensity_transform java line_descriptor mcc ml objdetect optflow ovis phase_unwrapping photo plot quality rapid reg rgbd saliency shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab viz wechat_qrcode xfeatures2d ximgproc xobjdetect xphoto
--     Disabled:                    world
--     Disabled by dependency:      -
--     Unavailable:                 cvv julia matlab python2 python3 sfm
--     Applications:                tests perf_tests apps
--     Documentation:               doxygen javadoc
--     Non-free algorithms:         NO
-- 
--   GUI:                           GTK3
--     GTK+:                        YES (ver 3.24.33)
--       GThread :                  YES (ver 2.72.1)
--       GtkGlExt:                  YES (ver 1.2.0)
--     OpenGL support:              YES (/usr/lib/x86_64-linux-gnu/libOpenGL.so /usr/lib/x86_64-linux-gnu/libGLX.so /usr/lib/x86_64-linux-gnu/libGLU.so)
--     VTK support:                 YES (ver 9.1.0)
-- 
--   Media I/O: 
--     ZLib:                        /usr/lib/x86_64-linux-gnu/libz.so (ver 1.2.11)
--     JPEG:                        /usr/lib/x86_64-linux-gnu/libjpeg.so (ver 80)
--     WEBP:                        /usr/lib/x86_64-linux-gnu/libwebp.so (ver encoder: 0x020f)
--     PNG:                         /usr/lib/x86_64-linux-gnu/libpng.so (ver 1.6.37)
--     TIFF:                        /usr/lib/x86_64-linux-gnu/libtiff.so (ver 42 / 4.3.0)
--     JPEG 2000:                   OpenJPEG (ver 2.4.0)
--     OpenEXR:                     /usr/lib/x86_64-linux-gnu/libImath-2_5.so /usr/lib/x86_64-linux-gnu/libIlmImf-2_5.so /usr/lib/x86_64-linux-gnu/libIex-2_5.so /usr/lib/x86_64-linux-gnu/libHalf-2_5.so /usr/lib/x86_64-linux-gnu/libIlmThread-2_5.so (ver 2_5)
--     HDR:                         YES
--     SUNRASTER:                   YES
--     PXM:                         YES
--     PFM:                         YES
-- 
--   Video I/O:
--     DC1394:                      YES (2.2.6)
--     FFMPEG:                      YES
--       avcodec:                   YES (58.134.100)
--       avformat:                  YES (58.76.100)
--       avutil:                    YES (56.70.100)
--       swscale:                   YES (5.9.100)
--       avresample:                NO
--     GStreamer:                   YES (1.20.1)
--     v4l/v4l2:                    YES (linux/videodev2.h)
-- 
--   Parallel framework:            TBB (ver 2021.5 interface 12050)
-- 
--   Trace:                         YES (with Intel ITT)
-- 
--   Other third-party libraries:
--     Intel IPP:                   2020.0.0 Gold [2020.0.0]
--            at:                   /mnt/microbbeloth/projects/opencv/build/3rdparty/ippicv/ippicv_lnx/icv
--     Intel IPP IW:                sources (2020.0.0)
--               at:                /mnt/microbbeloth/projects/opencv/build/3rdparty/ippicv/ippicv_lnx/iw
--     VA:                          YES
--     Lapack:                      YES (/usr/lib/x86_64-linux-gnu/liblapack.so /usr/lib/x86_64-linux-gnu/libcblas.so /usr/lib/x86_64-linux-gnu/libatlas.so)
--     Eigen:                       YES (ver 3.4.0)
--     Custom HAL:                  NO
--     Protobuf:                    build (3.19.1)
-- 
--   NVIDIA CUDA:                   YES (ver 11.5, CUFFT CUBLAS FAST_MATH)
--     NVIDIA GPU arch:             35 37 50 52 60 61 70 75 80 86
--     NVIDIA PTX archs:
-- 
--   cuDNN:                         YES (ver 8.2.4)
-- 
--   OpenCL:                        YES (INTELVA)
--     Include path:                /mnt/microbbeloth/projects/opencv/3rdparty/include/opencl/1.2
--     Link libraries:              Dynamic load
-- 
--   Python (for build):            /usr/bin/python3
-- 
--   Java:                          
--     ant:                         /bin/ant (ver 1.10.12)
--     JNI:                         /usr/lib/jvm/default-java/include /usr/lib/jvm/default-java/include/linux /usr/lib/jvm/default-java/include
--     Java wrappers:               YES
--     Java tests:                  YES
-- 
--   Install to:                    /usr/local
-- -----------------------------------------------------------------
-- 
-- Configuring done
-- Generating done
-- Build files have been written to: /mnt/microbbeloth/projects/opencv/build
[ 1%] Linking CXX shared library ../../lib/libopencv_cudev.so
/usr/bin/ld: cannot find -llib: No such file or directory
/usr/bin/ld: cannot find -llib: No such file or directory
collect2: error: ld returned 1 exit status
make[2]: *** [modules/cudev/CMakeFiles/opencv_cudev.dir/build.make:99: lib/libopencv_cudev.so.4.6.0] Error 1
make[1]: *** [CMakeFiles/Makefile2:4114: modules/cudev/CMakeFiles/opencv_cudev.dir/all] Error 2
make: *** [Makefile:166: all] Error 2

I’m not sure where to look to troubleshoot this problem. Any help you can offer would be appreciated. Thank you.

Michael

related issue:

also, i’m not sure, but this

-DCUDNN_LIBRARY=/usr/lib/x86_64-linux-gnu/lib

is a path to a folder, and it might need the name of the cudnn lib inside that

There was indeed a typo there. However, even removing the CUDNN library and include entries, it will still allow me to include CUDNN, but it will now modify external dependencies to show -L/usr/lib/x86_64-linux-gnu and -L/usr/lib and it chokes again with the linker saying that tit cannot find -lx86_64-linux-gnu. I’m using cmake 3.22 and trying to force either internal or external CMake makes no difference.

you misunderstand.

the CUDNN_LIBRARY variable must not be a path. it must be the name of a library. you filled this variable with the wrong value initially, and now you still give it the wrong value, only without a trailing /lib… which is why there’s the linker complaining about -lx86_64-linux-gnu… because you said (didn’t you?) -DCUDNN_LIBRARY=/usr/lib/x86_64-linux-gnu, which is wrong.

1 Like