Google Test

From GnuCash
Jump to: navigation, search

Installing Google Test for Building Gnucash

It is not generally recommended to install Google Test as shared libraries since C++ has a One Definition Rule where if two source files contain different definitions of the same class/function/variable and they are linked together the rule is violated, the linker may not detect the error and the behaviour at run time becomes unpredictable. See Google FAQs. If Google Test and your test code have been compiled with different compiler flags and because the Google Test code contains conditional #if statements mutiple definitions may be produced. To avoid this, Google recommend building the libraries as static libraries rather than dynamic shared libraries. That said GnuCash does build successfully with shared libraries.

Googletest v1.8.0

V1.8.0 of Googletest has googlemock incorporated into it rather than as a separate library. They can also be built as shared libraries.

Compiling Google Test and Google Mock into project code (recommended method)

In a terminal change directory to a suitable directory/folder in your home directory where you want to keep a local copy of googletest, e.g. $HOME/.local/src is one possibility used here for illustration. If you wish to place googletest elsewhere just substitute the path to your selected directory in the following. The static libraries are only built here to test the installation and are not used in practice.

cd $HOME/.local/src
git clone https://github.com/google/googletest.git
cd googletest
mkdir mybuild
cd mybuild
cmake -DBUILD_GMOCK=ON ../       #building gmock builds gtest by default
make                             # build the static libraries
# the following commands will create environment variables which if set and installed shared or static libraries are not detected will allow CMake to locate the sources and compile them into the project build.
# These environment variables can be made permanent by copying these commands into $HOME/.profile
export GTEST_ROOT=$HOME/.local/src/googletest

Installing Googletest as shared libraries

In a terminal change directory to a suitable directory, e.g. $HOME/Downloads. Substitute the path to another directory of your choice in the following if you wish.

cd $HOME/Downloads
git clone https://github.com/google/googletest.git
cd googletest
mkdir mybuild
cd mybuild
#building gmock builds gtest by default. Set the switches to generate the installation scripts for make
cmake -DCMAKE_INSTALL_PREFIX=/usr/local -DBUILD_SHARED_LIBS=ON -DBUILD_GMOCK=ON -DINSTALL_GTEST=ON -DINSTALL_GMOCK=ON ../       
make                             # build the static and shared libraries libraries
sudo make install

If you have installed the shared libraries do not define GTEST_ROOT and remove any existing commands to define it from $HOME/.profile.

The CMake files in v3.0 or 3.1 of GnuCash were not setup to detect the presence of the installed libraries. The changes in the patch need to be incorporated in the files ~/gnucash3.x/common/cmake-modules/GncAddTest.cmake and ~/gnucash3.x/common/test-core/CMakeLists.txt as discussed in the patch if you wish to use installed libraries. This patch is incorporated for V3.2 forward.

Some Linux distributions may have Google Test available to install from the distribution Software Manager, usually as shared libraries. Provided it is >=V1.8.0 this is the easiest method to install Google Test. If you have compile problems involving Google Test because of the issue mentioned above you may need to uninstall it and use the first method described below.

Google Test V1.8.0 may also be available from the apt repository (or equivalent) for your distribtion. If it is it may be installed as follows:

sudo apt install googletest

both these install the Google test 1.8.0 package in /usr/src/googletest which has subdirectories googletest and google mock. The cmake scripts in this installation don't appear to build GTEST by default when GMOCK is built as above. I may just have built them in the wrong order but after building and installing GTEST, GMOCK was still not found by the GnuCash cmake script but was after building both GTEST and GMOCK separately. Open a shell at /usr/src/googletest and execute the following commands:

cd googletest
sudo mkdir mybuild
cd mybuild
sudo cmake -DCMAKE_INSTALL_PREFIX=/usr/local -DBUILD_SHARED_LIBS=ON -DBUILD_GMOCK=ON -DINSTALL_GTEST=ON -DINSTALL_GMOCK=ON ../
sudo make
sudo make install
cd ..
cd ..
cd googlemock
sudo mkdir mybuild
cd mybuild
sudo cmake -DCMAKE_INSTALL_PREFIX=/usr/local -DBUILD_SHARED_LIBS=ON -DBUILD_GMOCK=ON -DINSTALL_GTEST=ON -DINSTALL_GMOCK=ON ../       
sudo make
sudo make install


As of V3.0,3.1, 3.2 building GnuCash with shared libraries is known to work on Ubuntu 16.04/Linux Mint 18.3/Linux Mint 19 but cannot be guaranteed on all systems.