DEPARTMENT OF COMPUTING

Resources: Programming in C++

Installing GoogleTest (Unit Test Framework for C++)

Ubuntu-like Linux (including Windows Subsystem for Linux)

Use apt to install the correct packages

sudo apt update
sudo apt install libgtest-dev

YOU ARE DONE. DON’T DO ANYTHING ELSE.

Mac OS X, or other OS from source code (at a Unix prompt)

These instructions are only for people with Mac OS X, or people who want to do more work and install from source code on Linux. If you did the sudo apt installation above, do not do these steps.

Visit the release page. Download the latest release (choose zip or tar.gz). This is currently 1.14.0 (Fall 2023). In the instructions below, replace 1.14.x with the current release of google test.

C++ Versions: As of Fall 2023, we’re still using the 2011 standard of C++, so you should actually use the most recent version that supports C++11, which is 1.12.1.

Unpack the source code. You must run this command in the directory where the archive file was downloaded.

unzip googletest-1.12.1.zip
or 
tar -zxf googletest-1.12.1.tar.gz

Move into the source tree:

cd googletest-1.12.1/googletest

Compile the source code, and build a library:

g++ -std=c++11 -pthread -g -Wall -Wextra -Werror -I. -I./include -c src/gtest-all.cc
ar rv libgtest.a gtest-all.o
g++ -std=c++11 -pthread -g -Wall -Wextra -Werror -I. -I./include -c src/gtest_main.cc
ar rv libgtest_main.a gtest_main.o

Make sure that sudo will run as root. This command may prompt you for your password. Then, it should display root. If you run it second time, it should not prompt you for your password. Then, you are ready to move on to the next step.

sudo whoami

Install the library for use:

sudo mkdir -p /usr/local/lib
sudo mv libgtest.a /usr/local/lib/libgtest.a
sudo chmod 644 /usr/local/lib/libgtest.a
sudo mv libgtest_main.a /usr/local/lib/libgtest_main.a
sudo chmod 644 /usr/local/lib/libgtest_main.a
sudo mkdir -p /usr/local/include
sudo mv include/gtest /usr/local/include
sudo chmod -R o+r /usr/local/include/gtest

For Linux/Windows:

sudo chown -R root:root /usr/local/include/gtest
sudo chown root:root /usr/local/lib/libgtest.a
sudo chown root:root /usr/local/lib/libgtest_main.a

For MacOS

sudo chown -R root:wheel /usr/local/include/gtest
sudo chown root:wheel /usr/local/lib/libgtest.a
sudo chown root:wheel /usr/local/lib/libgtest_main.a

Remove the source code, it’s no longer needed.

cd ../..
rm -r googletest-1.12.1
rm googletest-1.12.1.zip
or
rm googletest-1.12.1.tar.gz 

Last Updated 10/02/2023