Install googletest on OpenBSD

To use googletest on OpenBSD, I need to install CMake firstly:

# pkg_add cmake

Then Download googletest and create build directory:

# git clone https://github.com/google/googletest.git
# cd googletest
# mkdir build
# cd build

If use default gcc compiler, the compilation will generate following errors:

# make
[  2%] Building CXX object googlemock/CMakeFiles/gmock.dir/__/googletest/src/gtest-all.cc.o
cc1plus: warnings being treated as errors
In file included from /usr/include/g++/tr1/tuple:159,
                 from /root/Project/googletest/googletest/include/gtest/internal/gtest-port.h:746,
                 from /root/Project/googletest/googletest/include/gtest/internal/gtest-internal.h:40,
                 from /root/Project/googletest/googletest/include/gtest/gtest.h:58,
                 from /root/Project/googletest/googletest/src/gtest-all.cc:39:
/usr/include/g++/tr1/functional: In constructor 'std::tr1::_Mem_fn<_Res _Class::*>::_Mem_fn(_Res _Class::*)':
/usr/include/g++/tr1/functional:470: warning: declaration of '__pm' shadows a member of 'this'
In file included from /usr/include/g++/tr1/tuple:159,
                 from /root/Project/googletest/googletest/include/gtest/internal/gtest-port.h:746,
                 from /root/Project/googletest/googletest/include/gtest/internal/gtest-internal.h:40,
                 from /root/Project/googletest/googletest/include/gtest/gtest.h:58,
                 from /root/Project/googletest/googletest/src/gtest-all.cc:39:
/usr/include/g++/tr1/functional: In constructor 'std::tr1::_Simple_type_wrapper<_Tp>::_Simple_type_wrapper(_Tp)':
/usr/include/g++/tr1/functional:804: warning: declaration of '__value' shadows a member of 'this'
*** Error 1 in . (googlemock/CMakeFiles/gmock.dir/build.make:63 'googlemock/CMakeFiles/gmock.dir/__/googletest/src/gtest-all.cc.o': cd /root...)
*** Error 1 in . (CMakeFiles/Makefile2:90 'googlemock/CMakeFiles/gmock.dir/all')
*** Error 1 in /root/Project/googletest/build (Makefile:139 'all')

So I switch to clang, and all build is OK:

# CC=clang cmake ..
# make
# make install

The “***Exception: Illegal” error of running googletest

Today, a college told me his project testcases (using googletest) run failed with following errors:

1/6 Test #1: AddTest ....................***Exception: Illegal  1.70 sec
......

I am not familiar with his work nor an expert of googletest, but interested about this exception. After some searching, I bumped into the words from thisĀ post:

Given that the GROMACS build system enabled AVX2 SIMD on your VM which seems to not support anything above SSE2, it’s not a surprise that the first math instruction crashes the run.

Immediate solution: set -DGMX_SIMD=SSE2 when configuring.

So my buddy seemed meet the similar problem. After discussing with him, he confirmed his server is also a virtual machine and his issue is the same root cause. To satisfy my own curiosity, I download tfhe whose testcases use AVX and FMA while my machine can only support SSE. Run “make test“:

# make test
Running tests...
Test project /root/Project/tfhe/build
    Start 1: unittests-nayuki-portable
1/4 Test #1: unittests-nayuki-portable ........   Passed    8.59 sec
    Start 2: unittests-nayuki-avx
2/4 Test #2: unittests-nayuki-avx .............***Exception: Illegal  2.88 sec
    Start 3: unittests-spqlios-avx
3/4 Test #3: unittests-spqlios-avx ............***Exception: Illegal  2.86 sec
    Start 4: unittests-spqlios-fma
4/4 Test #4: unittests-spqlios-fma ............***Exception: Illegal  2.85 sec

25% tests passed, 3 tests failed out of 4

Total Test time (real) =  17.19 sec

The following tests FAILED:
          2 - unittests-nayuki-avx (ILLEGAL)
          3 - unittests-spqlios-avx (ILLEGAL)
          4 - unittests-spqlios-fma (ILLEGAL)
Errors while running CTest
make: *** [Makefile:95: test] Error 8

We can see “Exception: Illegal” errors are reported again.

Build googlemock library

When enabling ENABLE_TESTS option to build tfhe project, the googlemock library is needed, otherwise the following errors will be generated:

[ 54%] Linking CXX executable unittests-fftw
/usr/bin/ld: cannot find -lgmock
collect2: error: ld returned 1 exit status
make[2]: *** [test/CMakeFiles/unittests-fftw.dir/build.make:229: test/unittests-fftw] Error 1
make[1]: *** [CMakeFiles/Makefile2:1290: test/CMakeFiles/unittests-fftw.dir/all] Error 2
make: *** [Makefile:95: all] Error 2

To build googlemock, you can follow this guide:

(1) Download googletest:

$ git clone https://github.com/google/googletest.git

(2) Execute autoreconf command:

$ cd googletest/googlemock
$ autoreconf -fvi

(3) Create libgmock:

$ cd make
$ make
$ ar -rv libgmock.a gtest-all.o gmock-all.o

(4) Copy libgmock.a into /usr/local/lib:

$ cp libgmock.a /usr/local/lib/

Then you can use libgmock.a now.