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