I have installed google test as it'is described here. But when I try to use tests for my current project, I get 2 LNK4098 warnings:
defaultlib 'MSVCRTD' conflicts with use of other libs; use /NODEFAULTLIB:library
and the same for 'LIBCMTD', and a bunch of LNK2005 errors. But when I actually ignore these two default libraries, it doesn't help: I get even more errors. What's the problem?
Add a Google Test project in Visual Studio 2022In Solution Explorer, right-click on the solution node and choose Add > New Project. Set Language to C++ and type test in the search box. From the results list, choose Google Test Project. Give the test project a name and choose OK.
Go to Google test downloaded repo, extract it and navigate to: googletest →include →gtest [ex C:\Users\Downloads\googletest-release-1.10. 0\googletest-release-1.10. 0\googletest\include\gtest]. Copy that whole gtest file and copy to the folder MingW\lib\gcc\x86_64-w64-mingw32\8.1.
Google Test is not header-only: there are libraries to build.
You have to ensure googletest and your project are built using the same version of the C Runtime Library (CRT). Google test (currently v1.6.0) provides 2 Visual Studio solution files; gtest-1.6.0\msvc\gtest.sln
which uses the static version and gtest-1.6.0\msvc\gtest-md.sln
which uses the dynamic (dll) version. By default, projects created via Visual Studio use the dll version.
You need to decide whether you want your project to use the static or dynamic versions of the CRT.
To set your project to use the static versions, go to Project->Properties
and at the top left of the window, select Configuration: Debug
. Then in this same window select Configuration Properties
-> C/C++
-> Code Generation
. The option for Runtime Library
should be Multi-threaded Debug (/MTd)
.
You then need to ensure you're linking to the appropriate versions of gtest, so select Configuration Properties
-> Linker
-> Input
. Edit the Additional Dependencies
field by providing the full path to the Debug version of the gtest library (e.g. C:\gtest-1.6.0\msvc\gtest\Debug\gtestd.lib
).
Do the same again for Release Configuration, but setting the Runtime Library
option to Multi-threaded (/MT)
and providing the full path to the Release version of the gtest library (e.g. C:\gtest-1.6.0\msvc\gtest\Release\gtest.lib
).
If you decide you want to use the dll versions of the CRT, choose Multi-threaded Debug DLL (/MDd)
and Multi-threaded DLL (/MD)
, and link to the gtest-md libraries which will be in gtest-1.6.0\msvc\gtest-md\...
rather than gtest-1.6.0\msvc\gtest\...
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With