Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running gtest using Visual Studio 2010: LNK4098 and LNK2005

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?

like image 259
Yury Pogrebnyak Avatar asked Mar 29 '12 15:03

Yury Pogrebnyak


People also ask

How do I run a Gtest in Visual Studio?

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.

How install Gtest in Windows?

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.

Is Gtest header only?

Google Test is not header-only: there are libraries to build.


1 Answers

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\....

like image 59
Fraser Avatar answered Sep 23 '22 02:09

Fraser