12
votes

I have a project which links to half a dozen libraries, among them OpenCV.
Since Release variant is crashing, while Debug is working fine (just a lot slower), I wanted to compile my project in RelWithDebInfo configuration.
However, Debug version of OpenCV libraries gets included instead of Release (OpenCV doesn’t have RelWithDebInfo variant). This causes linking errors such as:

opencv_core249d.lib(alloc.obj) : error LNK2038: mismatch detected for ‘RuntimeLibrary’: value ‘MDd_DynamicDebug’ doesn’t match value ‘MD_DynamicRelease’ in MyProject.obj

How to solve this problem?

3
I solve this by never using the INSTALL target in windows. Instead point the PROJECTNAME_DIR variables to the root folder where you built the library. I also build all dependencies with the compiler I am using and never use binary packages. If you build opencv from source does it still eliminate the RelWithDebInfo configuration? - drescherjm
I have built OpenCV from source. I did not have this problem with pre-compiled OpenCV. - Dženan

3 Answers

21
votes

Solution: add to CMakeLists.txt, after the call to FIND_PACKAGE(OpenCV):

set_target_properties(${OpenCV_LIBS} PROPERTIES MAP_IMPORTED_CONFIG_RELWITHDEBINFO RELEASE)
3
votes

I've used the above answer by Dženan but found that some opencv libraries ignore this and still link debug! This results in an msvc project that links mixed release and debug libraries for opencv. My solution was to create a new OpenCVModules-relwithdebinfo.cmake in your the opencv installation's lib dir, which is a copy of the OpenCVModules-release.cmake file. Then replacing all references to RELEASE with RELWITHDEBINFO. This produces a msvc project linking entirely to opencv release libs. The added benefit is that you don't need to change your project's CMake files to account for this.

0
votes

you could disable level 2 optimizations with the default cmake release build using the below snippet. This would still pick release opencvworld (build with full optimizations) from the un-optimized (yet Release) app code. Using this we can step through the app's code line-by-line with full variable visibility.. as good as the debug build minus the need to link with debug opencv or mixing the CRTs or doing a full debug build of all components involved.

if (WIN32)

  SET (CMAKE_CXX_FLAGS_RELEASE "/Zi /Od")

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

endif (WIN32)