C++ CMake Debug Configuration (Visual Studio 2019)

Hi Folks,

I asked this question in the MSDN forum thinking it’s how Visual Studio deals with release/debug configuration specifically rather than a generic CMake thing. I also don’t think its a googletest specific thing either, but I was recommended to ask here so hopefully someone has some experience and can help out a noob.

I have a CPP project set up in Visual Studio 2019. I have successfully added pugixml and spdlog, compiled and debugged A-OK.

My process in general is using CMake GUI to generate, opening the generated project in VS2019, running ‘INSTALL’ target, which builds and installs as expected. This has all worked so far.

In my project CMakeLists.txt I simply add

set(spdlog_DIR "c:/cpp/spdlog/lib/cmake/spdlog")
find_package(spdlog REQUIRED)

set(pugixml_DIR "c:/cpp/pugixml/lib/cmake/pugixml")
find_package(pugixml REQUIRED)

I am now seeking to add googletest (GTest). I have added the following (after discovering I needed to use ROOT not DIR and point to the root directory.

set(GTest_ROOT "c:/cpp/googletest") 
find_package(GTest REQUIRED)

However, the GTest project INSTALL target builds the files with a ‘d’ suffix (as I have selected ‘Debug’ / ‘x64’ Solution Configuration).

  • gtest_maind.lib
  • gtest_maind.pdb
  • gtestd.lib
  • gtestd.pdb

So when I save my CMakeLists.txt file, I get the error

Could NOT find GTest (missing: GTEST_LIBRARY GTEST_MAIN_LIBRARY)

If I manually remove the ‘d’ suffixes from the files, the error goes away.

In the folder

c:\cpp\googletest\lib\cmake\GTest

There is the following files

  • GTestConfigVersion.cmake
  • GTestConfig.cmake
  • GTestTargets.cmake
  • GTestTargets-debug.cmake

The GTestTargets-debug.cmake contains the following:

# Import target "GTest::gtest" for configuration "Debug"
set_property(TARGET GTest::gtest APPEND PROPERTY IMPORTED_CONFIGURATIONS DEBUG)
set_target_properties(GTest::gtest PROPERTIES
  IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "CXX"
  IMPORTED_LOCATION_DEBUG "${_IMPORT_PREFIX}/lib/gtestd.lib"
  )

which is referencing the ‘d’ suffixed filename.

My CMakeSettings.json has
CMAKE_BUILD_TYPE set to Debug
Also my Project Configuration (via the Visual Studio interface) is set to x64-Debug (default).

Is there something additional I must do to get the GTestTargets-debug.cmake to be ‘read/parsed’? I’m assuming that this would point CMake to the ‘d’ equivalent lib file.

Many Thanks!

From the top of my head - maybe:
find_package(GTest REQUIRED CONFIG)?

1 Like

Perfect! I will read through the CMake documentation on this, but do you have a high level explanation as to why this worked?

CMake ships with a FindGTest module. It seems that the logic in this is not set up to handle a build without release artifacts. Using CONFIG makes it use the GTest-provided GTestConfig.cmake files which describes exactly what is available without erroring about the lack of release artifacts.

1 Like