Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"LNK2001 unresolved symbol" Erros about "cv::" Classes and Methods When Building a C++ Project with OpenCV and CUDA #25599

Closed
3 of 4 tasks
mericgeren opened this issue May 16, 2024 · 2 comments
Labels
category: build/install question (invalid tracker) ask questions and other "no action" items here: https://forum.opencv.org

Comments

@mericgeren
Copy link

mericgeren commented May 16, 2024

System Information

System Specifications:

  • Windows 10

  • Visual Studio 2022 (v143)

  • MSVC 14

  • C++14

  • CMake 3.29.3

  • 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>

Detailed description

When trying to build a C++project on Release mode and for x64, I see more than 20 errors like the following:

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

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.

Steps to reproduce

  1. Install Visual Studio 2022 and install ".NET desktop development", "Desktop development with C++" and "Universal Windows Platform development" workloads.

  2. Install CUDA 11.6.2 without NSight VSE, NSight Systems, NSight Compute and NSight NVTX.

  3. Install CMake 3.29.3 and add it to the path for all users while installing.

  4. Make sure that "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6\bin", "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6\lib" and "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6\lib\x64" are added the the PATH system variable.

  5. Download zips for opencv and opencv_contrib from "https://github.com/opencv/opencv/archive/refs/heads/3.4.zip" and "https://github.com/opencv/opencv_contrib/archive/refs/heads/3.4.zip". Then, extract both zip files under the same directory.

  6. Open CMake GUI. Write the directory where opencv is extracted to "Where is the source code:" and then, write "/build" to "Where to build the binaries".

  7. Press configure.

  8. Select "Visual Studio 17 2022" for "Specify the generator for this project" and leave other fields blank. Then click "Finish"

  9. After the configuration, unselect "BUILD_PROTOBUF", "BUILD_JAVA" and "WITH_PROTOBUF". Then, select "WITH_CUDA". Then, enter "/modules" to "OPENCV_EXTRA_MODULES_PATH".

  10. Open FindCUDA.cmake at "<where oğencv is extracted to>/cmake" with a notepad.

  11. Delete the line reads find_cuda_helper(nppi) under if(CUDA_VERSION_GREATER "7.5") and write the following to it's place


find_cuda_helper_libs(nppial)
find_cuda_helper_libs(nppicc)
find_cuda_helper_libs(nppicom)
find_cuda_helper_libs(nppidei)
find_cuda_helper_libs(nppif)
find_cuda_helper_libs(nppig)
find_cuda_helper_libs(nppim)
find_cuda_helper_libs(nppist)
find_cuda_helper_libs(nppisu)
find_cuda_helper_libs(nppitc)

  1. Still inside FindCUDA.cmake, find the line starts with set(CUDA_nppi_LIBRARY and delete ${CUDA_nppi_LIBRARY}; and write
    ${CUDA_nppial_LIBRARY};${CUDA_nppicc_LIBRARY};${CUDA_nppicom_LIBRARY};${CUDA_nppidei_LIBRARY};${CUDA_nppif_LIBRARY};${CUDA_nppig_LIBRARY};${CUDA_nppim_LIBRARY};${CUDA_nppist_LIBRARY};${CUDA_nppisu_LIBRARY};${CUDA_nppitc_LIBRARY}; in it's place.

  2. Save and close the FindCUDA.cmake file.

  3. Back in CMake GUI, click on "Generate"

  4. After the generation is successfull, go to "/build". Then, find and open OpenCV.sln file with Vision Studio 2022.

  5. Select build mode as Release and build platform as x64

  6. From projct explorer, find "ALL_BUILD". Then, right-click on "ALL_BUILD". Then, select "Compile".

  7. After build is successfull. Do the same to "INSTALL" which is just under "ALL_BUILD".

  8. After that build is also successfull, close the visual studio. Then create a c++ project with c++ (.cpp) and CUDA (.cu) code.

  9. use cv::countNonZero(), cv::merge and cv::cvtColor methods on CUDA (.cu) code and use cv::cvtColor, cvSaveImage, cv::Mat::setTo and cv::imwrite methods in C++ code.

  10. Then, save and close the project and Visual Studio.

  11. Find and open .vcxproj file with notepad. Then, edit necessary lines referring to the lines from TheopenCVProject.vcxproj file. Which I have provided under "System Specifications" section.

  12. Save and close the file. Then, find and open OpenCV.sln file with Vision Studio 2022.

  13. Make sure that build mode is release and build platform is x64.

  14. Build the project.

  15. After a while (less than a minute) see that there are several "LNK2001 unresolved external symbol" errors about methods and classes beginning with "cv::". Also obseve that all the errors have "debug_build_guard" and a strange mixture of "@" symbol and seemingly random letters like: "@N@1@AEBV".

Issue submission checklist

  • I report the issue, it's not a question
  • I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
  • I updated to the latest OpenCV version and the issue is still there
  • There is reproducer code and related data files (videos, images, onnx, etc)
@mericgeren mericgeren added the bug label May 16, 2024
@mericgeren mericgeren closed this as not planned Won't fix, can't repro, duplicate, stale May 20, 2024
@mericgeren mericgeren reopened this May 20, 2024
@opencv-alalek opencv-alalek added question (invalid tracker) ask questions and other "no action" items here: https://forum.opencv.org category: build/install and removed bug labels May 20, 2024
@opencv-alalek
Copy link
Contributor

debug_build_guard

Use searching: #9161

It is a protection against mess of debug/release binaries. Dump full compiler command line parameters and verify passed "DEBUG"-like definitions to compiler.

@mericgeren
Copy link
Author

Thank you for replying. I have found a workaround. I have added debug libraries to external dependencief of the project. Also, I got the same issue when building for debug and solved the isue in same way I used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
category: build/install question (invalid tracker) ask questions and other "no action" items here: https://forum.opencv.org
Projects
None yet
Development

No branches or pull requests

2 participants