Ask Your Question
0

Debugging OpenCV DLL used by Executable

asked 2018-07-06 03:59:19 -0600

mfischer-gundlach gravatar image

Hey there,

this question is partly related to OpenCV and partly debugging an executable in general, though the latter case is not really part of this forum. This is a long post, but I have the feeling it is better to elaborate on this, as the devil is in the details, and most likely a single lib/ dll is missing etc.

I am working with VS 2013 and have an executable, let's name it main, which uses OpenCV functionality. I want to debug into one of the functions, more precisely computeDisparitySGBM in stereoSGBM.cpp. For this reason I build mainin debug configuration and specify it in my OpenCV VS project (see https://docs.microsoft.com/en-us/visu...).

The trouble starts earlier, as I can not build the debug configuration of main (I can build release though) and receive a bunch of linker errors:

error LNK2019: unresolved external symbol "void __cdecl cv::imshow(class cv::String const &,class cv::debug_build_guard::_InputArray const &)" (?imshow@cv@@YAXAEBVString@1@AEBV_InputArray@debug_build_guard@1@@Z) referenced in function main

error LNK2019: unresolved external symbol "class cv::debug_build_guard::_InputOutputArray const & __cdecl cv::noArray(void)" (?noArray@cv@@YAAEBV_InputOutputArray@debug_build_guard@1@XZ) referenced in function "struct std::pair<class cv::mat,class="" cv::mat=""> __cdecl misc::composeStereoRT(class cv::Mat const &,class cv::Mat const &,class cv::Mat const &,class cv::Mat const &)" (?composeStereoRT@misc@@YA?AU?$pair@VMat@cv@@V12@@std@@AEBVMat@cv@@000@Z)

error LNK2019: unresolved external symbol "void __cdecl cv::Rodrigues(class cv::debug_build_guard::_InputArray const &,class cv::debug_build_guard::_OutputArray const &,class cv::debug_build_guard::_OutputArray const &)" (?Rodrigues@cv@@YAXAEBV_InputArray@debug_build_guard@1@AEBV_OutputArray@31@1@Z) referenced in function "struct std::pair<class cv::mat,class="" cv::mat=""> __cdecl misc::composeStereoRT(class cv::Mat const &,class cv::Mat const &,class cv::Mat const &,class cv::Mat const &)" (?composeStereoRT@misc@@YA?AU?$pair@VMat@cv@@V12@@std@@AEBVMat@cv@@000@Z)

Obviously, the linker does not find the objects, though it does not complain about other functions as imread. The project containing main was generated using CMake and the dependencies for release and debug config. are identically:

C:\Users\local_user\Documents\OpenCV\opencv-3.4.1\build\install\x64\vc12\lib\opencv_stitching341.lib C:\Users\local_user\Documents\OpenCV\opencv-3.4.1\build\install\x64\vc12\lib\opencv_superres341.lib C:\Users\local_user\Documents\OpenCV\opencv-3.4.1\build\install\x64\vc12\lib\opencv_videostab341.lib C:\Users\local_user\Documents\OpenCV\opencv-3.4.1\build\install\x64\vc12\lib\opencv_aruco341.lib C:\Users\local_user\Documents\OpenCV\opencv-3.4.1\build\install\x64\vc12\lib\opencv_bgsegm341.lib C:\Users\local_user\Documents\OpenCV\opencv-3.4.1\build\install\x64\vc12\lib\opencv_bioinspired341.lib C:\Users\local_user\Documents\OpenCV\opencv-3.4.1\build\install\x64\vc12\lib\opencv_ccalib341.lib C:\Users\local_user\Documents\OpenCV\opencv-3.4.1\build\install\x64\vc12\lib\opencv_dnn_objdetect341.lib C:\Users\local_user\Documents\OpenCV\opencv-3.4.1\build\install\x64\vc12\lib\opencv_dpm341 ... (more)

edit retag flag offensive close merge delete

Comments

you have to build DEBUG opencv libs / dll's and link against those for your DEBUG project. (e.g. opencv_stitching341**d**.lib

(those cv::debug_build_guards are there to show you the problem)

and yep, VS only shows the 1st (few) errors here, there are for sure more of the same problem

berak gravatar imageberak ( 2018-07-06 04:10:05 -0600 )edit
1

@Ziri, the prebuilt opencv_world(d) libs are only for VS 2015/2017. for VS2013, ppl have to build their own.

berak gravatar imageberak ( 2018-07-06 04:15:18 -0600 )edit
1

Thanks . I didn't know that.

Ziri gravatar imageZiri ( 2018-07-06 04:25:23 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-07-09 04:44:38 -0600

mfischer-gundlach gravatar image

Introduction

By debugging a DLL (also called debgging into a DLL) we can inspect the inner workings of a DLL. As with any other code, debugging a DLL is only possible if we are in possession of the DLL's source code. Debugging a DLL however differs from debugging an executable. Since DLLs (like any libraries) do not have an entry point (the main function in C++ executables), they can not be executed on their own. Rather a calling application is specified, which calls the DLL's functionality we want to debug. For debugging the DLL, the calling application is executed, which calls the DLL and the debugger stops at requested break points. From here on, debugging a DLL is the same as debugging any other code.

We document debugging DLLs written in C++ and called by applications naively written in C++. For debugging, we use Visual Studio 2013 and show the process at the hand of OpenCV.

Debug Build of DLL

For debugging the DLL, first we need to create debug symbols. OpenCV comes with a complete debug configuration. This means, by simply changing the configuration tab to Debug, the build process will produce debug symbols and add a suffix (d) to the debug DLLs. If your DLL project has no propper debug configuration yet, follow this guide to set the needed properties.

Next, we need to add the debug DLL path to the system environment variable PATH. Otherwise, the calling application will crash as it can not find the required DLL. The DLL's path is the DLL's project output directory (right click the DLL project -> Properties -> Configuration Properties/General -> Output Directory). After changing PATH, do not forget to reopen any application which needs the new set of environment variables! Furthermore, we need to remember the output folder and name for the created .lib file. (Note, not all DLLs produce a .lib file in the build process, but OpenCV does. We will mention this case later.) For example, our Visual Studio OpenCV project outputs a .lib file, with the same name as the DLL but different file extension, into the folder C:\Users\local_user\Documents\OpenCV\opencv-3.4.1/build/lib/debug/. This .lib will be important when configuring the calling application.

Specifying the Calling Application

As mentioned, a DLL can not be executed on its own but needs to be called by an application. To specify the calling application, follow this guide.

Configuring the Calling Application

Finally, we need to configure the calling application, such that on execution the debug versions of the called DLL are used. For this reason we already added the DLL's path to PATH earlier. However, most likely not in keep with your first intuition, when building an executable which makes use of a DLL, we also need to specify the corresponding .lib file as additional dependencies. This brings us to the topic of implicit linking of DLLs.

Implicit DLL Linking

There exists two ways of linking to ... (more)

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-07-06 03:59:19 -0600

Seen: 1,553 times

Last updated: Jul 09 '18