# Using GTest with CMake and Visual Studio

Recently had a bit of trouble setting up GTest with Visual Studio using CMake. Simple fix but just wanted to write it down!

Installation

You will need to get a copy of googletest:

Build

Do an out of source build.

Ok notice the BUILD_SHARED_LIBS option? The was the source of the issue when I initially tried this.

It depends on your unit test project configuration. Whether you are using static or dynamic runtime. The googletest project is set by default to build static libraries. When you generate a Visual Studio project it defaults to dynamic runtime. This mismatch caused link errors.

To fix this googletest must build dynamic libraries (or set Visual Studio runtime to static, I chose the first option).

Open the generated Visual Studio solution and build the libraries.

If everything works out fine you can build the INSTALL project in the solution to install the libraries to the specified path.

Of course the .dll files need to either be in the application path or on the system path so they can be loaded. If not an exception will be thrown and cause the error MS3073.

Including GTest in CMake

Ok so the hard part is over. Now to include GTest in the CMake project.

Unit Test


#include <gtest/gtest.h>