I get "Unresolved external symbol" error when trying to build a C++ project with OpenCV and CUDA

I am trying to build a C++ project with OpenCV 3.4.20 that’s built with CUDA 11.6.2 on Visual Studio 2022 and on Windows 10. But, a few seconds after I begin to build the project, I see several errors such as this:

LNK2001	unresolved external symbol "int __cdecl cv::countNonZero(class cv::debug_build_guard::_InputArray const &)" (?countNonZero@cv@@YAHAEBV_InputArray@debug_build_guard@1@@Z)	

System Specifications:

  • Windows 10

  • Visual Studio 2022

  • OpenCV 3.4.20 (built with CUDA and opencv_contrib modules)

  • CUDA 11.6.2

  • System variables are such as these:

    • CUDA_PATH = C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6
    • CUDA_PATH_V11.6 = C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6
    •           PATH = C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6\bin
                       C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6\lib
                       C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6\lib\x64
                       C:\Users\RIT\opencv-3.4\build\bin\
                       C:\Users\RIT\opencv-3.4\build\lib\
        
      
  • Relevant lines from TheopenCVProject.vcxproj file:

    • Under PropertyGroup Label=“Globals”:

      • <CudaToolkitCustomDir>C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6</CudaToolkitCustomDir>
    • Under ImportGroup Label=“ExtensionSettings”:

      • <Import Project="$(VCTargetsPath)\BuildCustomizations\CUDA 11.6.props" />
    • Under PropertyGroup Condition=“‘(Configuration)|(Platform)’==‘Release|x64’”:

      • <IncludePath>C:\Users\RIT\opencv-3.4\build\install\include;$(IncludePath)</IncludePath>
      • <LibraryPath>C:\Users\RIT\opencv-3.4\build\install\x64\vc17\lib;$(LibraryPath)</LibraryPath>
    • Under ItemDefinitionGroup Condition=“‘(Configuration)|(Platform)’==‘Release|x64’”

      • <AdditionalIncludeDirectories>C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>

      • <AdditionalDependencies>opencv_cudabgsegm3420.lib;npps.lib;nppial.lib;nppicc.lib;nppidei.lib;nppif.lib;nppig.lib;nppim.lib;nppist.lib;nppisu.lib;nppitc.lib;nppc.lib;Gdiplus.lib;opencv_bgsegm3420.lib;opencv_videoio3420.lib;opencv_cudalegacy3420.lib;opencv_video3420.lib;opencv_core3420.lib;opencv_highgui3420.lib;cudart.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;cublas.lib;%(AdditionalDependencies)</AdditionalDependencies>

      • <AdditionalLibraryDirectories>C:\Users\RIT\opencv-3.4\build\install\x64\vc17\lib;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6\lib\x64;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>

      • Under CudaCompile:

        • <CodeGeneration>compute_86,sm_86</CodeGeneration>
        • <AdditionalLibraryDirectories>C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6\lib\x64;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6\bin\crt;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>

Notes:

  1. There are two folders under Users except Default and Public: RIT is the folder in which opencv, opencv_contrib and the folder of the project I want to build is located. The other one RİT is the default one with all the directories you might expect from a regular User directory such as AppData, Documents and Downloads are located.
  2. When I check opencv folders I can see that all needed libraries are there and I can see that the functions subject of errors are both declared and defined in their respective .cpp, .h and .hpp files.
  3. There are both release and debug libraries under the directory into which I have built the OpenCV.
  4. Tried to build the project in debug mode, enabling debug information with /Z7 option and disabling optimization flags to no avail.
  5. If I try to compile each .cu and .cpp file one by one, they all get built successfully.

CUDA Toolkit 11.6.2 (March 2022), Versioned Online Documentation

OpenCV – 3.4.0
2017-12-23

Good luck

Hi @laurent.berger

Would you mind taking a look at OpenCV 3.4.20 tag page and Opencv 3.4 branch head GitHub page?

OpenCV - 3.4.20
2023-06-27

Thanks

Ok my mistake but why don’t you use old opencv version?

Why don’t you use CMake to build VS project? opencv/samples/cpp/example_cmake at 4.x · opencv/opencv · GitHub

When I tried an older version (3.1.0), it proved incompatible with the system and the opencv itself failed to build.

To have ease of control over how and to where I build

Use cmake and check difference with your own project.

When I try to build with opencv I see several errors like the following:

LNK2019: unresolved external symbol nppiMulC_32f_C3IR referenced in function "enum cudaError __cdecl calcDev3C(float const *,int,int)" (?calcDev3C@@YA?AW4cudaError@@PEBMHH@Z)

Notes:

  1. Checked properties for each CUDA (.cu) file and item type was CUDA C/C++ for all CUDA files. Also, there was an object files (.cu.obj) for each CUDA file.

  2. Seeing “/MD” at Runtime library property under C/C++ section in properties of the .sln file.

Have you got problem to build opencv or your own project?

I have got problem to build my own project

using opencv 5 I have all those libs in my VS project

C:\install\opencv\x64\vc17\lib\opencv_gapi500.lib
C:\install\opencv\x64\vc17\lib\opencv_stitching500.lib
C:\install\opencv\x64\vc17\lib\opencv_alphamat500.lib
C:\install\opencv\x64\vc17\lib\opencv_bgsegm500.lib
C:\install\opencv\x64\vc17\lib\opencv_bioinspired500.lib
C:\install\opencv\x64\vc17\lib\opencv_ccalib500.lib
C:\install\opencv\x64\vc17\lib\opencv_cudabgsegm500.lib
C:\install\opencv\x64\vc17\lib\opencv_cudafeatures2d500.lib
C:\install\opencv\x64\vc17\lib\opencv_cudaobjdetect500.lib
C:\install\opencv\x64\vc17\lib\opencv_cudastereo500.lib
C:\install\opencv\x64\vc17\lib\opencv_dnn_objdetect500.lib
C:\install\opencv\x64\vc17\lib\opencv_dnn_superres500.lib
C:\install\opencv\x64\vc17\lib\opencv_dnnlegacy500.lib
C:\install\opencv\x64\vc17\lib\opencv_dpm500.lib
C:\install\opencv\x64\vc17\lib\opencv_face500.lib
C:\install\opencv\x64\vc17\lib\opencv_fuzzy500.lib
C:\install\opencv\x64\vc17\lib\opencv_hfs500.lib
C:\install\opencv\x64\vc17\lib\opencv_img_hash500.lib
C:\install\opencv\x64\vc17\lib\opencv_intensity_transform500.lib
C:\install\opencv\x64\vc17\lib\opencv_line_descriptor500.lib
C:\install\opencv\x64\vc17\lib\opencv_mcc500.lib
C:\install\opencv\x64\vc17\lib\opencv_my_module500.lib
C:\install\opencv\x64\vc17\lib\opencv_quality500.lib
C:\install\opencv\x64\vc17\lib\opencv_rapid500.lib
C:\install\opencv\x64\vc17\lib\opencv_reg500.lib
C:\install\opencv\x64\vc17\lib\opencv_rgbd500.lib
C:\install\opencv\x64\vc17\lib\opencv_saliency500.lib
C:\install\opencv\x64\vc17\lib\opencv_sfm500.lib
C:\install\opencv\x64\vc17\lib\opencv_signal500.lib
C:\install\opencv\x64\vc17\lib\opencv_structured_light500.lib
C:\install\opencv\x64\vc17\lib\opencv_superres500.lib
C:\install\opencv\x64\vc17\lib\opencv_surface_matching500.lib
C:\install\opencv\x64\vc17\lib\opencv_videostab500.lib
C:\install\opencv\x64\vc17\lib\opencv_viz500.lib
C:\install\opencv\x64\vc17\lib\opencv_wechat_qrcode500.lib
C:\install\opencv\x64\vc17\lib\opencv_xfeatures2d500.lib
C:\install\opencv\x64\vc17\lib\opencv_xphoto500.lib
C:\install\opencv\x64\vc17\lib\opencv_xstereo500.lib
C:\install\opencv\x64\vc17\lib\opencv_shape500.lib
C:\install\opencv\x64\vc17\lib\opencv_phase_unwrapping500.lib
C:\install\opencv\x64\vc17\lib\opencv_cudacodec500.lib
C:\install\opencv\x64\vc17\lib\opencv_cudaoptflow500.lib
C:\install\opencv\x64\vc17\lib\opencv_cudalegacy500.lib
C:\install\opencv\x64\vc17\lib\opencv_calib500.lib
C:\install\opencv\x64\vc17\lib\opencv_cudawarping500.lib
C:\install\opencv\x64\vc17\lib\opencv_optflow500.lib
C:\install\opencv\x64\vc17\lib\opencv_ximgproc500.lib
C:\install\opencv\x64\vc17\lib\opencv_xobjdetect500.lib
C:\install\opencv\x64\vc17\lib\opencv_objdetect500.lib
C:\install\opencv\x64\vc17\lib\opencv_photo500.lib
C:\install\opencv\x64\vc17\lib\opencv_cudaimgproc500.lib
C:\install\opencv\x64\vc17\lib\opencv_cudafilters500.lib
C:\install\opencv\x64\vc17\lib\opencv_cudaarithm500.lib
C:\install\opencv\x64\vc17\lib\opencv_stereo500.lib
C:\install\opencv\x64\vc17\lib\opencv_tracking500.lib
C:\install\opencv\x64\vc17\lib\opencv_highgui500.lib
C:\install\opencv\x64\vc17\lib\opencv_video500.lib
C:\install\opencv\x64\vc17\lib\opencv_3d500.lib
C:\install\opencv\x64\vc17\lib\opencv_videoio500.lib
C:\install\opencv\x64\vc17\lib\opencv_datasets500.lib
C:\install\opencv\x64\vc17\lib\opencv_imgcodecs500.lib
C:\install\opencv\x64\vc17\lib\opencv_plot500.lib
C:\install\opencv\x64\vc17\lib\opencv_text500.lib
C:\install\opencv\x64\vc17\lib\opencv_dnn500.lib
C:\install\opencv\x64\vc17\lib\opencv_features2d500.lib
C:\install\opencv\x64\vc17\lib\opencv_flann500.lib
C:\install\opencv\x64\vc17\lib\opencv_imgproc500.lib
C:\install\opencv\x64\vc17\lib\opencv_ml500.lib
C:\install\opencv\x64\vc17\lib\opencv_core500.lib
C:\install\opencv\x64\vc17\lib\opencv_cudev500.lib

I have these libs at C:\Users\RIT\opencv-3.4\build\install\x64\vc17\lib (Without Java, Python and Protobuf)

opencv_aruco3420.lib
opencv_aruco3420d.lib
opencv_bgsegm3420.lib
opencv_bgsegm3420d.lib
opencv_bioinspired3420.lib
opencv_bioinspired3420d.lib
opencv_calib3d3420.lib
opencv_calib3d3420d.lib
opencv_ccalib3420.lib
opencv_ccalib3420d.lib
opencv_core3420.lib
opencv_core3420d.lib
opencv_cudaarithm3420.lib
opencv_cudaarithm3420d.lib
opencv_cudabgsegm3420.lib
opencv_cudabgsegm3420d.lib
opencv_cudacodec3420.lib
opencv_cudacodec3420d.lib
opencv_cudafeatures2d3420.lib
opencv_cudafeatures2d3420d.lib
opencv_cudafilters3420.lib
opencv_cudafilters3420d.lib
opencv_cudaimgproc3420.lib
opencv_cudaimgproc3420d.lib
opencv_cudalegacy3420.lib
opencv_cudalegacy3420d.lib
opencv_cudaobjdetect3420.lib
opencv_cudaobjdetect3420d.lib
opencv_cudaoptflow3420.lib
opencv_cudaoptflow3420d.lib
opencv_cudastereo3420.lib
opencv_cudastereo3420d.lib
opencv_cudawarping3420.lib
opencv_cudawarping3420d.lib
opencv_cudev3420.lib
opencv_cudev3420d.lib
opencv_datasets3420.lib
opencv_datasets3420d.lib
opencv_dpm3420.lib
opencv_dpm3420d.lib
opencv_face3420.lib
opencv_face3420d.lib
opencv_features2d3420.lib
opencv_features2d3420d.lib
opencv_flann3420.lib
opencv_flann3420d.lib
opencv_fuzzy3420.lib
opencv_fuzzy3420d.lib
opencv_hfs3420.lib
opencv_hfs3420d.lib
opencv_highgui3420.lib
opencv_highgui3420d.lib
opencv_imgcodecs3420.lib
opencv_imgcodecs3420d.lib
opencv_imgproc3420.lib
opencv_imgproc3420d.lib
opencv_img_hash3420.lib
opencv_img_hash3420d.lib
opencv_line_descriptor3420.lib
opencv_line_descriptor3420d.lib
opencv_ml3420.lib
opencv_ml3420d.lib
opencv_objdetect3420.lib
opencv_objdetect3420d.lib
opencv_optflow3420.lib
opencv_optflow3420d.lib
opencv_phase_unwrapping3420.lib
opencv_phase_unwrapping3420d.lib
opencv_photo3420.lib
opencv_photo3420d.lib
opencv_plot3420.lib
opencv_plot3420d.lib
opencv_reg3420.lib
opencv_reg3420d.lib
opencv_rgbd3420.lib
opencv_rgbd3420d.lib
opencv_saliency3420.lib
opencv_saliency3420d.lib
opencv_shape3420.lib
opencv_shape3420d.lib
opencv_stereo3420.lib
opencv_stereo3420d.lib
opencv_stitching3420.lib
opencv_stitching3420d.lib
opencv_structured_light3420.lib
opencv_structured_light3420d.lib
opencv_superres3420.lib
opencv_superres3420d.lib
opencv_surface_matching3420.lib
opencv_surface_matching3420d.lib
opencv_tracking3420.lib
opencv_tracking3420d.lib
opencv_video3420.lib
opencv_video3420d.lib
opencv_videoio3420.lib
opencv_videoio3420d.lib
opencv_videostab3420.lib
opencv_videostab3420d.lib
opencv_xfeatures2d3420.lib
opencv_xfeatures2d3420d.lib
opencv_ximgproc3420.lib
opencv_ximgproc3420d.lib
opencv_xobjdetect3420.lib
opencv_xobjdetect3420d.lib
opencv_xphoto3420.lib
opencv_xphoto3420d.lib

I have found how to solve the issue:

  1. I have opened the .sln file of the project.
  2. Then from the topbar, clicked on “Project”.
  3. From the dropdown opened, clicked on “Properties TheopenCVProject”.
  4. In properties window selected “Linker” and "
    Link Library Dependencies".
  5. Then Added Debug libraries, methods of which are mentioned in error messsages.

After following these steps build was successful.

1 Like

"LNK2001 unresolved symbol" Erros about "cv::" Classes and Methods When Building a C++ Project with OpenCV and CUDA · Issue #25599 · opencv/opencv · GitHub ?