Dženan
Dženan

Reputation: 3395

CMake RelWithDebInfo links to Debug libs

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?

Upvotes: 13

Views: 6508

Answers (5)

Michiel Mooibroek
Michiel Mooibroek

Reputation: 36

I also had this issue; but None of the answers above solved it.

Eventually I solved it by putting these two lines at the start of my CMakeLists.txt:

cmake_policy(SET CMP0091 NEW)
# Make the project use static libraries (*.lib) instead of DLLs.
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Release>")

I think $CONFIG:Debug:Release is doing the magic...

Gr,

Michiel

Upvotes: 1

Ber
Ber

Reputation: 1

I really had problems with the "3rd Party libs" / OpenCVModules. @dženan 's Answer lead me into the right direction, but his solution didn't work out for me, however, adding this line before using FIND_PACKAGE solved my problem:

SET(CMAKE_MAP_IMPORTED_CONFIG_RELWITHDEBINFO "RelWithDebInfo;Release;")

Be aware, it will set a fallback to Release for everything.

Upvotes: 0

Neelabh Mam
Neelabh Mam

Reputation: 310

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)

Upvotes: 0

Daniel Moodie
Daniel Moodie

Reputation: 367

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.

Upvotes: 3

Dženan
Dženan

Reputation: 3395

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

set_target_properties(${OpenCV_LIBS} PROPERTIES MAP_IMPORTED_CONFIG_RELWITHDEBINFO RELEASE)

Upvotes: 22

Related Questions