CMake RelWithDebInfo links to Debug libs

2019-03-15 23:11发布

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?

2条回答
甜甜的少女心
2楼-- · 2019-03-15 23:46

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

set_target_properties(${OpenCV_LIBS} PROPERTIES MAP_IMPORTED_CONFIG_RELWITHDEBINFO RELEASE)
查看更多
放我归山
3楼-- · 2019-03-16 00:00

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.

查看更多
登录 后发表回答