Dark Sorrow
Dark Sorrow

Reputation: 1937

Switch between Debug and Release Library in cmake programmatically for vcpkg installed libraries

I have successfully compiled my C++ program but I am a getting linker error

(default)\Catch2.lib(catch_stringref.cpp.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.cpp.obj 

After reading the post I realized this is due to improper library selection by library (using release mode library while my application is in debug mode) hence I printed the path to catch2 library

CMake Code

find_library(CATCH2_LIBRARY catch2)
message ("CATCH2_LIBRARY : ${CATCH2_LIBRARY}")
target_link_libraries(${PROJECT_NAME} ${CATCH2_LIBRARY})

returned

CATCH2_LIBRARY : D:/vcpkg/installed/x64-windows/lib/Catch2.lib

instead of

D:/vcpkg/installed/x64-windows/debug/lib/Catch2d.lib

I wish to know how can I make the cmake look into the debug directory (for libraries) when I am building in debug mode and vice versa.

Kindly note in Debug Mode the library is Catch2d.lib and in release mode it is Catch2.lib.

Library Paths

Release Library Path : D:\vcpkg\installed\x64-windows\lib\Catch2.lib
Debug Library Path : D:\vcpkg\installed\x64-windows\debug\lib\Catch2d.lib

This is continuation of my first cmake usage saga carried forward from here

Upvotes: 1

Views: 918

Answers (2)

imphil
imphil

Reputation: 31

Make sure that you're setting the build type before compiling Catch2, i.e., place set(CMAKE_BUILD_TYPE Debug) at the very top of your CMakeLists.txt file.

Upvotes: 0

Milan Š.
Milan Š.

Reputation: 1638

What you are most likely interested in, is something that's called a generator expression. This allows you to define specific flags/names/suffixes/prefixes etc. based on the specified configuration.

They usually look something like this:

target_link_libraries(foobar PRIVATE Catch2$<$<CONFIG:Debug>:d>)

When running cmake with the Debug configuration it will add a d at the end of Catch2

Upvotes: 1

Related Questions