C++ - Google Test - Generating the gtest.lib from gtest.sln with Visual Studio

Google Test framework is provided with a solution already generated that we can use with Visual Studio.

It's the gtest.sln file.

But to use it we have to accomplish some steps in order to having it working properly.

This is what we will see in this Google Test tutorial.

Let's get started.

First of all

For this tutorial we need:

Google Test will be installed in this directory:

  • C:\dev\c++\mylib\googletest-release-1.8.0

Using files for Visual Studio

We are going to use the files generated directly by Google for Visual Studio, they are in this directory:

  • C:\dev\c++\mylib\googletest-release-1.8.0\googletest\msvc

You can see gtest.sln, double-click it to open the solution in your IDE.

You will be invited to One-way upgrade all your files, click OK.

But when you try to Rebuild Solution, you'll see the following error:

Severity Code Description Project File Line Suppression State

Error MSB8036 The Windows SDK version 8.1 was not found. Install the required version of Windows SDK or change the SDK version in the project property pages or by right-clicking the solution and selecting "Retarget solution". gtest_main C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\VC\VCTargets\Platforms\Win32\PlatformToolsets\v141\Toolset.targets 34 
Don't worry, there is a way to fix it.
Indeed we have to install 2 components:
  • Windows 8.1 SDK
  • Windows Universal CRT SDK

You have to run the vs_community.XXXXX.XXXXX.exe launcher (the one you use to install Visual Studio), you'll have the choice to Modify your Visual Studio Community 2017 IDE.

Or just simply do from Visual Studio > Tools > Get Tools and Features...

Click Modify.

You should have already installed the Desktop development with C++ components.

It's logically already checked.

Then, from the top of the launcher, you have 3 tabs:

  • Workloads
  • Individual components
  • Language packs

Select the Individual components tab.

OF COURSE, REALLY IMPORTANT: close all instances of Visual Studio before proceeding.

Otherwise, you won't be able to modify the installation.

From the list, at the end, you'll see the Windows 8.1 SDK (from the SDKs, libraries and frameworks part).

Its size is 298 MB.

Then we need another component, this time from the Compilers, build tools and runtimes part:

Select Windows Universal CRT SDK (this is the C RunTime which fixes some bugs).

Its size is 312 MB.

So for both components we have Install size: 610 MB.

Click Modify, just below, to install them.

The new installation is starting.

Wait until everything is installed and click Launch.

OK let's back to Visual Studio.

Generating the gtest.lib or gtestd.lib

Reopen the gtest.sln solution.

Then on the Solution Explorer, right click the Solution 'gtest' > Retarget solution > Select 8.1 from the Windows SDK Version combobox > OK.

Then Rebuild Solution.

This time, everything should be OK.

Depending if you built your solution in debug or release mode, you have now two new files:

  • C:\dev\c++\mylib\googletest-release-1.8.0\googletest\msvc\gtest\Debug\gtestd.lib
  • C:\dev\c++\mylib\googletest-release-1.8.0\googletest\msvc\gtest\Release\gtest.lib

It's important to note that for the debug output there is a "d" letter in gtestd.lib.

Generating the library was really important because it was the main purpose of this tutorial.

Running tests

There are two ways to run the tests, either with a CLI or with an adapter directly from Visual Studio.

With a command-line interface (CLI)

It's possible to launch the tests directly from a CLI such as Cygwin, PowerShell or the old cmd.

So as we've just building our gtest projects, we can now check that some executables have been created, you can see them in the following directory:

  • C:\dev\c++\mylib\googletest-release-1.8.0\googletest\msvc\gtest\Debug

So open your CLI in this directory and just type for example:

./gtest_prod_test.exe

The result:

Running main() from gtest_main.cc
[==========] Running 2 tests from 2 test cases.
[----------] Global test environment set-up.
[----------] 1 test from PrivateCodeTest
[ RUN      ] PrivateCodeTest.CanAccessPrivateMembers
[       OK ] PrivateCodeTest.CanAccessPrivateMembers (0 ms)
[----------] 1 test from PrivateCodeTest (1 ms total)

[----------] 1 test from PrivateCodeFixtureTest
[ RUN      ] PrivateCodeFixtureTest.CanAccessPrivateMembers
[       OK ] PrivateCodeFixtureTest.CanAccessPrivateMembers (0 ms)
[----------] 1 test from PrivateCodeFixtureTest (1 ms total)

[----------] Global test environment tear-down
[==========] 2 tests from 2 test cases ran. (3 ms total)
[  PASSED  ] 2 tests.

You can do the same with the gtest_unittest.exe file but result will be much longer.

With an adapter

This adapter is the Google Test Adapter.

It's not an official tool from Google, but it will help us to discover all available tests from the Google Test framework.

It's essential because without it, we won't be able to see them.

So from Visual Studio > Tools > Extensions and Updates... > On the left, click the Online section > Then on the right type Google Test Adapter in the search input.

The current version is 0.10.1.793, it adds support for the C++ unit testing framework Google Test.

Install it and close your Visual Studio, the extension might take many minutes to setup by VSIX.

Once done, reopen Visual Studio then > Test > Windows > Test Explorer.

You can see all tests discovered by the adapter.

To run them, just click Run All (in blue) above tests.

At the end of the run, you should have 433 tests421 passed and 13 skipped.

If yes, congratulations, you have just finished this tutorial.

Possible error with the Adapter

The 'GoogleTestExtensionOptionsPage' package did not load correctly. 

The problem may have been caused by a configuration change or by the installation of another extension. You can get more information by examining the file 'C:\Users\Username\AppData\Roaming\Microsoft\VisualStudio\VersionNumber\ActivityLog.xml'. 

Restarting Visual Studio could help resolve this issue. 

Continue to show this error message?

This error is due to the fact that you already installed a version of Google Test Adapter.

Maybe not exactly the same, there are indeed 2 different versions:

  • Test Adapter for Google Test
  • Google Test Adapter

They are the same except that the latter is up to date.

Conclusion

You are now ready to implement your own tests and check their results directly from Visual Studio.

Good job, once again, you did it. laugh

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.