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 Linux (Debian-based) operating system.
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:
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 Terminal by pressing the key combination, Ctrl + Alt + T. This will open the CLI. With the command-line interface open, you can type the command:
The sudo
command has been used as the /opt
directory requires administrator privileges when creating files and
folders. The outcome of this command will be a folder created in the /opt
directory called GoogleTest
.
To ensure there is read and write access to these directories, which is important for building and compilation, we need
to change the owner of these directories. To do this, we can use the chown
command, which will change the user and
group from root
to the user of your machine. This can be achieved by using the following commands:
The -R
flag will ensure that the change of ownership is applied recursively for each file and folder that exist
within the directories. Whilst the $USER
variable will automatically apply the username of the currently logged into
account as the owner of the directories and files.
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 /opt
. This can be achieved using the
following command:
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:
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:
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 /opt/googletest
.
Once the compilation process has completed, you should be met with an outcome that has the last few lines:
Important
If you are met with any other issue (or error), speak to the module team in the lab session for assistance.
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:
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:
The output may not be exactly the same, but as long as the last output reads 100%
and there were no errors or issues
then the libraries have been built successfully.
Once the libraries have been built, you are now required to install the libraries on the system. However, before you
can do this, you need to make
the files that have been built. Before you do this, you need to check the number of
processes that can be executed at once on your system. To do this, you need to run the following command:
The output of this command will be an integer number, and in the example above it is 20
. This is a value that you
need to use in the next command:
Make sure you replace the number above with the result of your nproc
command. This will start the make install
process, and it will enable the number of processes that can be run at a single time corresponding to the number that
you inserted. In this case, twenty process can be run at one time during the make process before a new process is
spawned.
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:
$ sudo make install -j20
-- Installing: /usr/local/lib/libgtest.a
-- Installing: /usr/local/lib/libgtest_main.a
-- Installing: /usr/local/lib/pkgconfig/gtest.pc
-- Installing: /usr/local/lib/pkgconfig/gtest_main.pc
The sudo
command has been used as the /usr/local
directory requires administrator privileges when creating files
and folders. Once the installation process has finished, the libraries will have been installed.
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.
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.