opencv / opencv

Open Source Computer Vision Library
https://opencv.org
Apache License 2.0
76.54k stars 55.64k forks source link

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

Closed mericgeren closed 3 weeks ago

mericgeren commented 1 month ago

System Information

System Specifications:

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)

12) 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.

13) Save and close the FindCUDA.cmake file.

13) Back in CMake GUI, click on "Generate"

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

15) Select build mode as Release and build platform as x64

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

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

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

19) 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.

18) Then, save and close the project and Visual Studio.

19) 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.

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

21) Make sure that build mode is release and build platform is x64.

22) Build the project.

24) 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

opencv-alalek commented 4 weeks ago

debug_build_guard

Use searching: https://github.com/opencv/opencv/pull/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 commented 3 weeks ago

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.