C++ CMake Debug Configuration (Visual Studio 2019)

CppForMe 106 Reputation points
2021-03-19T01:00:07.813+00:00

Hello,

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!

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,628 questions
Visual Studio Debugging
Visual Studio Debugging
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Debugging: The act or process of detecting, locating, and correcting logical or syntactical errors in a program or malfunctions in hardware. In hardware contexts, the term troubleshoot is the term more frequently used, especially if the problem is major.
966 questions
0 comments No comments
{count} votes

Accepted answer
  1. CppForMe 106 Reputation points
    2021-03-22T09:35:32.07+00:00

    I posted on the CMake forum and was suggested the following, which worked. Notice the CONFIG parameter that I was not previously using (nor was required for pugixml or spdlog). I am updating the thread incase anyone needs this here in the future.

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

    And all is well with the world. Here is the explanation, but I will read more into the CONFIG parameter.

    Link here: CMake Forum Post

    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.

    Worth noting that when generating googletest from CMake GUI I had to enabled

    gtest_force_shared_crt
    

    as I was getting a linker error. I found this here:

    https://github.com/google/googletest/tree/master/googletest

    Visual Studio Dynamic vs Static Runtimes

    By default, new Visual Studio projects link the C runtimes dynamically but GoogleTest links them statically. This will generate an error that looks something like the following: gtest.lib(gtest-all.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj

    GoogleTest already has a CMake option for this: gtest_force_shared_crt

    Enabling this option will make gtest link the runtimes dynamically too, and match the project in which it is included.

    Hope this helps someone.

    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Tianyu Sun-MSFT 29,351 Reputation points Microsoft Vendor
    2021-03-19T09:55:33.19+00:00

    Hello @CppForMe ,

    Welcome to Microsoft Q&A forum.

    From your description and the error message, this issue(could not find XXXX) should be caused by the codes or mechanism of searching and finding the specific files. I guess CMake or GoogleTest may provide some methods, ways to prevent this issue or to help VS(maybe) to find the files which have added the d suffixes.

    I suggest you post this issue on CMake or GoogleTest related forums, like CMake, GitHub googletest for better help.

    Best Regards,
    Tianyu

    • If the answer is helpful, please click "Accept Answer" and upvote it.
      Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
    1 person found this answer helpful.
    0 comments No comments

  2. CppForMe 106 Reputation points
    2021-03-20T14:00:18.393+00:00

    Thanks for this information @Tianyu Sun-MSFT for the links.

    I was under the impression that Visual Studio controls whether to build via Release/Debug via the interface and thus this is a Visual Studio configuration problem? I will investigate via the CMake forum.

    1 person found this answer helpful.
    0 comments No comments