Can I use a Release config of OpenCV with Debug config of my app?

Viewed 3711

I am writing a Universal Windows application that uses OpenCV to do camera calibration and tag detection, among other things.

I'd like to be able to use release mode, fully-optimized OpenCV DLLs while my own is in debug mode. Is this possible? If so, how do I configure CMake to make it happen?

I do want to debug my own application, naturally. However, I do not want to debug OpenCV itself. I do not want to deal with the performance drop of an OpenCV debug build, even while I'm debugging my own project. I do not want to deal with the overhead of the additional libs and dlls, and setting up my build configuration properly (this is part of a larger project, including a Unity app, so build configuration is a significant issue).

I'm using OpenCV DLLs, not static libs, so issues of differences in implementations of the STD library, etc. should not make any difference (unless OpenCV is passing those across the DLL boundary... which would be naughty indeed). Yet, when I try to build my app in Debug mode, while referencing OpenCV Release DLLs, I encounter the debug_build_guard:

error LNK2019: unresolved external symbol "bool __cdecl cv::imencode(class cv::String const &,class cv::debug_build_guard::_InputArray const &,...

It seems like what I want would be a really common use case. Unfortunately I can't seem to find anything about it anywhere. Thanks for any help!

3 Answers

Add CV_IGNORE_DEBUG_BUILD_GUARD as one of the preprocessor definitions, which is defined in /path/to/opencv/opencv2/core/cvdef.h

There are two kind of DLL/LIB in the build folder of the OpenCV.

Release DLL/LIB: In these files, the debug guard is activated. For example in version 4.01 the DLL is opencv_world401.dll and the associated LIB is opencv_world401.lib

Debug DLL/LIB: These are designed for debug perposes and the name of fine ends with d (e.g. opencv_world401d.dlland opencv_world401d.iib).

You need to add the debug DLL and the associated LIB to the Linker in the debug mode.

Instead of using CMAKE, I use #pragma to include my lib dynamically. Here is the method I used the preprocessor to solve this issue and load my lib dynamically.

#include <opencv2/opencv.hpp>            
#include "opencv2/core/version.hpp"
#include "opencv2/videoio/videoio.hpp"
#define OPENCV_VERSION CVAUX_STR(CV_VERSION_MAJOR)"" CVAUX_STR(CV_VERSION_MINOR)"" CVAUX_STR(CV_VERSION_REVISION)
#if NDEBUG 
#pragma comment(lib, "opencv_world" OPENCV_VERSION ".lib")
#else
#pragma comment(lib, "opencv_world" OPENCV_VERSION "d.lib")
#endif

For Release build configuration of the app you could disable optimizations. This would still pick the release opencvworld (build with full optimizations) from the un-optimized (Release CRT) app code. Using this we can step through the app's code line by line with full variable visibility. Something on the lines of below. The only thing that the default cmake Release flags differs in is I believe with the /O2 level optimizations as opposed to the /Od which we use here. I use it for debugging my app. I never do a debug build of my app and I never do a debug opencv build. Works fine.

if (WIN32)

  SET (CMAKE_CXX_FLAGS_RELEASE "/Zi /Od")

  SET_TARGET_PROPERTIES(
   xyz PROPERTIES 
   LINK_FLAGS 
   "/DEBUG /OPT:REF /OPT:ICF"
  )

endif (WIN32)
Related