1

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

Dark Sorrow
  • 1,681
  • 14
  • 37
  • If the only purpose of re-asking a [question asked a day ago](https://stackoverflow.com/questions/75105859/how-to-switch-between-debug-and-release-library-in-cmake-programmatically-for-vc) is to put the question on the front page, then it is not how Stack Overflow works. Instead you could use a [bounty system](https://stackoverflow.com/help/bounty) for attract the attention to your question. – Tsyvarev Jan 14 '23 at 16:11
  • As for the problem itself, you could look e.g. into module [FindZLIB.cmake](https://github.com/Kitware/CMake/blob/master/Modules/FindZLIB.cmake) shipped with CMake itself. Note on the searching for `ZLIB_LIBRARY_RELEASE` and `ZLIB_LIBRARY_DEBUG` libraries, and following call to `select_library_configurations()` macro (defined in `SelectLibraryConfigurations.cmake` module). – Tsyvarev Jan 14 '23 at 16:16

1 Answers1

1

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

Milan Š.
  • 1,353
  • 1
  • 2
  • 11
  • No. This is wrong. Because each dependency adds a `PackageNameTargets.cmake` into the `vcpkg_installed\your-triplet\share\package` folder. This file is responsible for including any `PackageTargets-*.cmake`, e.g. `PackageTarget-debug.cmake` and `PackageTarget-release.cmake.`. And these files contain the correct names to link the library (using `IMPORTED_LOCATION_*`). The name of the library in the `target_link_libraries()` directive does not change and is not appended by `d`! – Jens A. Koch Mar 24 '23 at 23:10