Help with using Release/Debug for external dependency (Visual Studio CMAKE)

Hi there,

Apologies up front, I’m still learning CMake and using Visual Studio probably isn’t helping learn the basics (using it as its so good at debugging code).

I have a CMake project in Visual Studio and I’m linking to spdlog (performance logger). I checked out spdlog and used CMake-GUI to generate the make files. Then opened the VS solution files to build/install. Worth noting that even though CMake-GUI had CMAKE_BUILD_TYPE set to Release, that when I opened the generated files in VS, it was defaulting to debug. So I built as Debug (which also installed a cmake folder of cmake files and a pkgconfig folder), toggled it to Release and rebuilt (that built only the release lib file, but didn’t install the cmake/pkgconfig folder oddly and it built it into the src folder of spdlog not the installation folder).

I dropped the release build into the installation folder created by the debug build.

In my project CMakeLists.txt, I have

set(spdlog_DIR "C:/Work/Libs/spdlog/lib/cmake/spdlog")  
find_package(spdlog REQUIRED)

target_link_libraries(my-project PRIVATE spdlog::spdlog)

I also tried this as a test

target_link_libraries(my-project PRIVATE optimized spdlog::spdlog) 

but that seemed to do nothing.

I can build my project and use spdlog fine if my project (via VS UI) is in x64-Debug, but when I switch to x64-Release, I get linker errors and I can see it’s trying to link to spdlogd.lib (debug) not the spdlog.lib (release). Linker errors are
error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '2' doesn't match value '0' in my-project.cpp.obj

I would have expected VS to pass the appropriate flags to CMake (or Ninja it seems - this bit confuses me) to link to the spdlog build type matching my project build type.

My spdlog installation folder is as so (as mentioned, I manually added the spdlog.lib into it, because the Release build only built the lib file - and in the src directory not the installation directory).

│   spdlog.lib
│   spdlogd.lib
│   └───spdlog
│           spdlogConfig.cmake
│           spdlogConfigTargets-debug.cmake
│           spdlogConfigTargets.cmake
│           spdlogConfigVersion.cmake

I could ask in an spdlog forum, but thought I’d try here first in case I’m doing something daft.
Any assistance greatly appreciated.

Did you also drop the lib/cmake files the release install made into the same install tree? You should just be able to install both the release and debug configurations of spdlog to the same directory.

So… embarrassing. But yes, when I dropped in the spdlogConfigTargets-release.cmake file as well, it worked.

Side question, do you know if it’s possible to add a CMake-Gui parameter to change the output name? I’m also using pugixml and regardless of if it’s a Release or Debug, the output .lib name is the same (whereas spdlog suffixes a ‘d’ for the debug). I have renamed it manually and updated the cmake output debug file with the new filename, but wondered if there is a better way?

Thank you again.

Assuming it’s not overridden internally, the CMAKE_DEBUG_POSTFIX variable will initialize the DEBUG_POSTFIX property.

1 Like