Sunday, March 6, 2016

Google Test in Visual Studio

This post is for those who just need "a refresh" of the steps required to set google test with Visual Studio. If you are a newbie, please refer the excellent references at the bottom of this post.

Some simple actions will allow you to use the popular Google Test Framework in Visual Studio.
In a summary you will need to perform these steps:

1) Download the latest version of google test
2) Open and Compile in Visual Studio the TWO Visual Studio solutions you'll find in googletest/msvc: gtest.sln (static libraries) and gtest-md.sln (dynamic libraries)
NOTE: Before open gtest.sln and gtest-md.sln remember to unlock these files (right click and unblock).
3) Neglect the warnings in Visual Studio and
With the DEBUG  configuration  Build -> Build Solution
With the Release configuration : Build -> Build Solution

These steps will create the gtest libraries in msvc/gtest/Debug  msvc/gtest/Release.
Debug:
gtest_maind.lib
gtestd.lib

Release:
gtest_main.lib
gtest.lib

NOTE the extra "d" in the Debug libraries (e.g. gtestd.lib vs gtest.lib)

4) To create a test project to support an existing project of yours you'll need to make the following changes in your test project:
  • Be sure your Runtime Library is the same in BOTH project AND it is consistent with the google test libraries you are using: e.g. if you compiled gtest.sln YOU need to set a multi-thread runtime environment : /MT and /MTd , for release and debug solution, respectively.  This is set in General Properties -> C/C++ -> Code Generation. Distinguish between Release and Debug
  • In  General Properties set correctly Include Directories and Libraries Directories
  • In  General Properties -> Linker -> Input-> Additional Dependencies  add the previously generated google test libraries at point 3. Distinguish between Release and Debug.
  •  Make your test project dependent on your NON-test project. Select your test project from the solution explorer. Right Click -> Build Dependencies -> Project Dependencies : Your NON-Test project should be listed among the available projects.

That's it.

NOTE:
You have some linker errors like "Linker error LNK2038: mismatch detected for 'RuntimeLibrary'..."
Be sure that:
  • Runtime Libraries match between your TEST and NON-Test projects
  • The gtest library you are linking is consistent with the runtime Library you chose:  /MT  and /MTd require gtest_main.lib/gtest_maind.lib (static libraries) and  gtest.lib/gtestd.lib (static libraries) while /MD and /MDd require gtest.lib /gtestd.lib (dynamic versions) and gtest_main-md.lib / gtest_main-mdd.lib (dynamic versions).



References:

No comments:

Post a Comment

Your comment will be visible after approval.