This is a continuation of the blog entry on a quick start guide to setting up Google Test, this time specific to Visual Studio.
First Things First
If you used the quick start guide to set up Google Test, you may need to be aware of a bug in Visual Studio that occurs when using tests in static libraries (or DLLs) with Google Test. Google provides some simple information on what you need to know and how to work around the bug. You should read and follow those instructions before doing anything more. If you plan to create any tests in a static library (or DLL), the extra steps documented there will probably be required in order for your tests to run.
Adding a Project
Here’s how to add a Google Test project to the Visual Studio solution that you created when you used CMake in the quick start guide.
In Visual Studio, open the solution and add a new project of type Visual C++ > Windows Desktop > Windows Console Application or Static Library. In the new project’s properties, change the following settings:
- Configuration Properties > C/C++ > General > “Additional Include Directories”: Add
- Configuration Properties > C/C++ > General > “Warning Level”: Choose
- Configuration Properties > C/C++ > General > “Treat Warnings as Errors”: Choose
- Configuration Properties > C/C++ > Code Generation > “Runtime Library”: Depending on release/debug configuration, choose either
Multi-threaded (/MT) or Multi-threaded Debug (/MTd)
- Configuration Properties > C/C++ > Precompiled Headers > “Precompiled Headers”: Choose
Not Using Precompiled Headers
- Configuration Properties > C/C++ > Precompiled Headers > “Precompiled Header File”: Remove any filename, so that this setting is blank.
If the project you added is a Windows console application (executable):
Expand the project name in the Solution Explorer window, and right click on References, and choose “Add Reference…” and select gtest, gtest_main, and any other static library projects from the solution that this project will depend upon. [At the time of this writing, the Google Test CMake creates a “gtest” static library/project that contains all the essential google test library functions, and a “gtest_main” static library/project that contains the main() function.]
In this project’s properties, set the following:
- Configuration Properties > Linker > General > “Link Library Dependencies”: Choose
- Configuration Properties > Linker > Optimization > “References”: Choose
There are a few different ways you can get Google Test and set it up so you can begin to use it. Strangely, the Google Test documentation skims over some of these basic starting steps, while providing choice overload for some other steps. It’s still the best reference, but it’s not a great quick start guide. You might find Google Test integrated into some of your tools (for example Visual Studio 17) – so that may be easier to use than this guide, depending on your needs. The method in this guide gives you both flexibility and control over how you use Google Test.
My own initial interest was setting up Google Test on Windows, and so this guide adds Windows specific details in parentheses, which may help clarify some steps regardless of OS. Aside from the parenthetical information, this guide should (hopefully) work for any OS.
- Get the git repository for googletest.
- Open a command prompt and navigate to the directory where you want the googletest repository to exist (On Windows, I created a new directory for repositories, called C:\Users\Jeff\Documents\repositories, which I then cd’d into)
- On the command line, type
git clone https://github.com/google/googletest.git
- Set an environment variable called GTEST_DIR to the path of the new “googletest” directory that was just created by git clone.
(On Windows, to reflect my own googletest path, I set GTEST_DIR to C:\Users\Jeff\Documents\repositories\googletest. You should set GTEST_DIR as a permanent environment variable, which on Windows you can do via Control Panel > System > Advanced system settings > Environment Variables… then “New…” under System variables. If you still have the command window open, you’ll need to relaunch it in order to use the GTEST_DIR variable.)
- If you don’t have CMake, download and install it.
- For an expanded version of this final step, see Building Google Test as a Standalone CMake Project.
On the command line, navigate to a directory where you would like to place a new build of Google Test. On the command line, type
Then if you are on Unix or OSX, type
cmake -Dgtest_build_samples=ON $GTEST_DIR
If you are on Windows, type
cmake -Dgtest_build_samples=ON %GTEST_DIR%
If you don’t want any of the google test sample projects, you can omit -Dgtest_build_samples=ON from the cmake command above.
You should now have Google Test built on your system. To go from here, if you are using Visual Studio, make sure you read Google Test projects in Visual Studio. Otherwise, and in general, I’d suggest to read the Google Test documentation.