Search code examples
c++windowsopencvbuildvisual-studio-2022

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.

Solution

  • I realized that, the project actually needs both Release and Debug libraries for Release build. So, I have followed these steps:

    1. I have opened TheopenCVProject.sln file with Visual Studio.

    2. From the top bar of VS, I have selected Project.

    3. Then, I have clicked on Properties from dropdown menu under Project.

    4. From properties window, I have selected Linker.

    5. In Linker section, I have edited the list of Link Library Dependencies.

    6. I have added libraries of methods mentioned in errors to the list of Link Library Dependencies.

      Let's say we got this error: LNK2001 unresolved external symbol "int __cdecl cv::countNonZero(class cv::debug_build_guard::_InputArray const &)" (?countNonZero@cv@@YAHAEBV_InputArray@debug_build_guard@1@@Z) and we use 3.4.20 version of OpenCV.

      According to documentation of OpenCV countNonZero method is declared in Core.hpp, so we should add opencv_core3420d.lib to the list of Link Library Dependencies.