CUEH Development Environment Guides
Compiling GoogleTest Libraries

Building and Installing the GoogleTest Library

As part of the programming module you study on the course, you will be tasked with using the C++ programming language to complete lab activities and individual programming assessments. However, you will also neeed to provide a method of testing your work by using a unit testing framework. The library for testing in C++ used on this course uses the GoogleTest library.

In this guide, you shall be taken through the journey of building and installing the GoogleTest library for the Microsoft Windows operating system.

Pre-Requirements for this Guide

Before you can move ahead and build and install the GoogleTest library, you will need the required tools: git and a C++ toolset. To ensure you have these on your system, you will want to follow these guides:

Cloning the GoogleTest Libraries

To use the GoogleTest libraries on your machine, it will be necessary to clone the repository provided on the GitHub website. To do this, open Developer Command Prompt for VS 2022 by searching for cmd in the Start Menu. With the command-line interface open, you can type the command:

> git clone https://github.com/google/googletest C:\googletest

This will clone the GoogleTest libraries to the root C:\ drive to a directory called googletest.

Configuring the GoogleTest Libraries

With the GoogleTest libraries cloned to your machine, you can now begin the process of configuring the library. Before you can begin, you will need to traverse the directory googletest located in C:\. This can be achieved using the following command:

> cd C:\googletest

Inside the googletest directory, you will need to create a folder called build. This is where we shall build the library from source. This can be achieved using the following command:

> mkdir build && cd build

Once the directory has been created, the command will automatically put you inside the build folder, and you can now begin the process of building the library. The building process will make use of the CMake tool that was installed during the process of setting up your C++ toolset. The following command can be used to configure the GoogleTest library:

> cmake .. -D gtest_force_shared_crt=ON

The .. at the end of the command tells the cmake tool to build GoogleTest using the library files that are located in the directory up-one from the build directory. Therefore, it will use the files that are located in the root of the directory C:\googletest.

Once the compilation process has completed, you should be met with an outcome that has the last few lines:

> cmake .. -D gtest_force_shared_crt=ON
  -- Build files have been written to: C:/googletest/build

Important

If you are met with any other issue (or error), speak to the module team in the lab session for assistance.

Building the GoogleTest Library

With the GoogleTest library now configured, you can begin the process of building the library. In order to do this, you must ensure you are still located in the build directory and use the following command:

> cmake --build .

This will begin the process of building the GoogleTest library. Depending on the capability of your machine, this process may take some time. However, upon its completion, you should be met with an outcome similar to below:

> cmake --build .
  Building Custom Rule C:/googletest/CMakeLists.txt

The output may not be exactly the same, but as long as the last output reads similar and there were no errors or issues then the libraries have been built successfully.

Installing the GoogleTest Library

Once the libraries have been built, you are now required to install the libraries on the system. To do this, you need to run the following command:

> cmake --build . --target INSTALL

Important

It is very important you are running the Developer Command Prompt for VS 2022 in administrator mode. If you do not, then the installation process will error.

This will install the GoogleTest library to the folder we disclosed in the configuration process, in this case /usr/local. You should be met with an outcome similar to the one provided below:

> cmake --build . --target INSTALL
  -- Installing: C:/Program Files (x86)/googletest-distribution/lib/gtest.lib
  -- Installing: C:/Program Files (x86)/googletest-distribution/lib/gtest_main.lib
  -- Installing: C:/Program Files (x86)/googletest-distribution/lib/pkgconfig/gtest.pc
  -- Installing: C:/Program Files (x86)/googletest-distribution/lib/pkgconfig/gtest_main.pc

Once the installation process has finished, the libraries will have been installed. However, we need to rename the directory where the library was installed to. You will notice that the library has been placed in a directory called googletest-distribution. However, this is not the folder that the cmake tool will look for when compiling your C++ unit tests. Instead, it will be looking for a folder called GTest. Therefore, to resolve this issue, we need to rename the folder googletest-distribution to GTest. To achieve this, you can use the following command:

> cd "C:\Program Files (x86)" && ren googletest-distribution GTest

Important

It is very important you are running the Developer Command Prompt for VS 2022 in administrator mode. If you do not, then the installation process will error.

The above command will move you to the directory called Program Files (x86) and then rename the folder googletest-distribution to GTest.

Conclusion

That is the end of this guide on building, compiling and installing the GoogleTest libraries. If you have followed all the necessary steps correctly, you should have a fully functional implementation of the GoogleTest unit test framework. This will enable you to complete any necessary programming based lab activities and assessments that you will participate with on this course.

Mistakes or Problems?

If you have spotted any errors or issues within this tutorial, you can e-mail Dr Ian Cornelius. Ensure to include in your message a description of the error or issue and a possible resolution. Also remember to include a URL to the page with the issue or error.