1

I'm having trouble setting up a googletest unit test project with Visual Studio 2019 for an existing project.

I started by choosing New > Project from VS and choosing googletest. The project stub that was created by VS compiles and runs fine.

I added the project I'm trying to test to the unit test's References. The project is in the same solution.

When I tried testing one of my classes, the test compiled fine. The linker couldn't find my class, though. So I added the .obj file from the other project to the unit test project's linker dependencies (Properties > Linker > Additional dependencies). Now I get another linker error:

gtestd.lib(gtest-all.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary':
value 'MDd_DynamicDebug' doesn't match value 'MTd_StaticDebug'

I can't change the settings of the project I'm trying to test because of various reasons (incompatibilities with other libraries). So I changed the settings of the unit tests project (Properties > C/C++ > Code Generation > Runtime Library). However, I still get the same linker error, probably because gtestd.lib itself is fixed to MDd_DynamicDebug. How can I change that?

I searched the web, and all I found is some information on how you would do this with CMake, but not with Visual Studio.

digory doo
  • 1,978
  • 2
  • 23
  • 37
  • Short answer is that you can't – john Aug 15 '19 at 09:22
  • Longer answer is that you need to rebuild the googletest libraries using the C++ code generation switches you want. I assume this is possible but I've never done it myself. – john Aug 15 '19 at 09:25
  • @john It is actually the first thing to do when starting using google test – user7860670 Aug 15 '19 at 09:33
  • I thought if VS2019 comes with googletest out of the box, it should be possible to do this in the IDE. I'll have a look at how to build googletest with CMake, then. – digory doo Aug 15 '19 at 09:47
  • You have to regenerate the gtest MSVC project using CMake (see dupe, preferred solution). Alternatively, update the CRT mode for gtest manually in Project Settings -> C/C++ -> Code Generation -> Runtime Library. – rustyx Aug 15 '19 at 10:41
  • This is *not* a duplicate, because my question was about Visual Studio and specifically not about CMake. In fact, I was able to solve it without having to build googletest. In VS, simply choose "Link statically" in the dialog that appears when creating the project, that's all. – digory doo Aug 15 '19 at 14:17
  • That's not what you said in the question: "*I can't change the settings of the project I'm trying to test... So I changed the settings of the unit tests project (Properties > C/C++ > Code Generation > Runtime Library). However, **I still get the same linker error***". If linking statically works, then what's the issue? – rustyx Aug 15 '19 at 14:53
  • I can't change the settings of the project I'm testing. I *can* change the settings of the unit test project. I was looking for these settings and couldn't find them. My solution was to delete the unit test project and start all over again, because here I am given the choice. – digory doo Aug 15 '19 at 15:01

0 Answers0