Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV won't build with CUDA even though WITH_CUDA=ON in CMake

I have tried to build OpenCV 3.1 using CMake (the gui version) to enable Cuda. I have installed Cuda version 7.5 64-bit and CMake automatically found the correct path to the Cuda toolkit. I made sure that the WITH_CUDA value was set to ON, and pressed configure. This is what I got concerning Cuda:

CUDA detected: 7.5
CUDA NVCC target flags: -gencode;arch=compute_20,code=sm_20;-gencode;arch=compute_20,code=sm_21;-gencode;arch=compute_30,code=sm_30;-gencode;arch=compute_35,code=sm_35;-gencode;arch=compute_30,code=compute_30

...

Extra dependencies:          comctl32 gdi32 ole32 setupapi ws2_32 vfw32 cudart nppc nppi npps cufft -LC:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v7.5/lib/x64

...

Other third-party libraries:
Use IPP:                     9.0.1 [9.0.1]
     at:                     C:/OpenCV-3.1.0/opencv/sources/3rdparty/ippicv/unpack/ippicv_win
Use IPP Async:               NO
Use Eigen:                   NO
Use Cuda:                    YES (ver 7.5)
Use OpenCL:                  YES
Use custom HAL:              NO

NVIDIA CUDA
Use CUFFT:                   YES
Use CUBLAS:                  NO
USE NVCUVID:                 NO
NVIDIA GPU arch:             20 21 30 35
NVIDIA PTX archs:            30
Use fast math:               YES

Then I generate using Visual Studio 12 2013 Win64.

Next I open the newly generated OpenCV.sln project in Visual Studio 2013 and build the project. It completes without any errors, but 103 warnings like this:

LINK : warning LNK4044: unrecognized option '/LC:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v7.5/lib/x64'; ignored

OpenCV builds just fine and I can build programs with it. I can also include "opencv2/core/cuda.hpp" without any problem. However, when I try to use cuda::getDevice() i get this error:

OpenCV Error: No CUDA support (The library is compiled without CUDA support) in throw_no_cuda, file C:\builds\master_PackSlave-win64-vc12-shared\opencv\modules\core\include\opencv2/core/private.cuda.hpp, line 97

When I print the information from cv::getBuildInformation() i get:

Other third-party libraries:
Use IPP:                     9.0.1 [9.0.1]
     at:                     C:\builds\master_PackSlave-win64-vc12-shared\opencv\3rdparty/ippicv/unpack/ippicv_win
Use IPP Async:               NO
Use Eigen:                   NO
Use Cuda:                    NO
Use OpenCL:                  YES
Use custom HAL:              NO

It seems Cuda is disabled somewhere in the process, but I can't figure out why. I have tried to reconfigure and rebuild several times with the same results. Would love some help on this!

like image 556
markustp Avatar asked Jan 06 '23 08:01

markustp


1 Answers

The problem is CMake: it generates wrong link options for Visual Studio.

LINK : warning LNK4044: unrecognized option '/LC:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v7.5/lib/x64'; ignored

It should be "LIBPATH:C:" instead of "LC:".

To fix the problem:

  • Generate the solution with CMake
  • Go to the build directory
  • Open an IDE/Text editor which is able to do a global search in this directory
  • For each occurrence of "-LC:" in any file, replace it by "-LIBPATH:C:".

========= EDIT =========

I found the problem, it is in CMakeLists.txt :

foreach(p ${CUDA_LIBS_PATH})
  set(OPENCV_LINKER_LIBS ${OPENCV_LINKER_LIBS} -L${p})
endforeach()

Instead of using "${CMAKE_LIBRARY_PATH_FLAG}" which automatically put -LIBPATH, someone put "-L"...

So to fix it:

  • Open CMakeLists.txt
  • Replace "-L" by "${CMAKE_LIBRARY_PATH_FLAG}"

Configure & Generate the solution with CMake and compile with VS.

Hope it will help!

like image 179
Dubrzr Avatar answered Jan 17 '23 19:01

Dubrzr