question

CppForMe-4069 avatar image
0 Votes"
CppForMe-4069 asked ·

C++ CMake Debug Configuration (Visual Studio 2019)

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++vs-debugging
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

CppForMe-4069 avatar image
1 Vote"
CppForMe-4069 answered ·

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.


·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

TianyuSun-MSFT avatar image
1 Vote"
TianyuSun-MSFT answered ·

Hello @CppForMe-4069 ,

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.


·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

CppForMe-4069 avatar image
1 Vote"
CppForMe-4069 answered ·

Thanks for this information @TianyuSun-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.

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.