Setting up googletest on macOS

I am making strong assumption that you have XCode and Command Line Tools installed.

First of all, you need CMake. I prefer installation from the sources, so I go this way.

> mkdir -p $HOME/opt/src
> cd $HOME/opt/src
> curl -o https://github.com/Kitware/CMake/releases/download/v3.16.2/cmake-3.16.2.tar.gz
> tar zxf cmake-3.16.2.tar.gz
> cd cmake-3.16.2
> ./bootstrap --prefix=$HOME/opt/usr/local
> make
> make install

Make sure to have $HOME/opt/usr/local/bin on your PATH.

After you have CMake in place, you can build googletest.

> cd $HOME/opt/src
> git clone https://github.com/google/googletest.git
> cd googletest
> mkdir build
> cd build

# If you don't specify -DBUILD_SHARED_LIBS=ON you will get only static libs (.a)
> cmake -DCMAKE_INSTALL_PREFIX:PATH=$HOME/opt/usr/local -DBUILD_SHARED_LIBS=ON ..
> make
> make install

And now, you can test the whole thing

// simple.h
int sum(int a, int b);
// simple.cc
int sum(int a, int b) {
  return a + b;
}
// simple_test.cc
#include "simple.h"
#include "gtest/gtest.h"

TEST(SumTest, Equal) {
  EXPECT_EQ(2, sum(1,1));
}

Once, everything is in place, we can run the code

> g++ -std=c++11 -o test \
  simple.cc simple_test.cc \ 
  -I. -I$HOME/opt/usr/local/include \
  $HOME/opt/usr/local/lib/libgtest.a \
  $HOME/opt/usr/local/lib/libgtest_main.a
> ./test
Running main() from $HOME/opt/src/googletest/googletest/src/gtest_main.cc
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from SumTest
[ RUN      ] SumTest.Equal
[       OK ] SumTest.Equal (0 ms)
[----------] 1 test from SumTest (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (0 ms total)
[  PASSED  ] 1 test.

If you want to use libgtest.dylib and libgtest_main.dylib you will have to play a little with with @rpath.

# Make sure that google test itself has proper settings with rpath
> install_name_tool -add_rpath ${HOME}/opt/usr/local/lib \
    ${HOME}/opt/usr/local/lib/libgtest_main.dylib

> install_name_tool -change libgtest.dylib \
    @rpath/libgtest.dylib ${HOME}/opt/usr/local/lib/libgtest_main.dylib

# Compile your code
> g++ -std=c++11 -o test \
  simple.cc simple_test.cc \ 
  -I. -I$HOME/opt/usr/local/include \
  -L$HOME/opt/usr/local/lib -rpath $HOME/opt/usr/local/lib -lgtest -lgtest_main

and now, make sure to update @rpath inside your executable

> install_name_tool -change libgtest.dylib @rpath/libgtest.dylib
> install_name_tool -change libgtest_main.dylib @rpath/libgtest_main.dylib ./test

Alternative approach. Change the id of libs

> install_name_tool -id @rpath/libgtest_main.dylib $HOME/opt/usr/local/lib/libgtest_main.dylib
> install_name_tool -id @rpath/libgtest.dylib $HOME/opt/usr/local/lib/libgtest.dylib

in this case, you don’t have to rename libs inside your final executable.

And all of this started here, with this tutorial session covering googletest basics: Unit testing in CLion

References:
DT_RPATH (ld) & @rpath (dyld)
Shared Libraries: Understanding Dynamic Loading
Fun with rpath, otool, and install_name_tool
Run-Path Dependent Libraries
Overview of Dynamic Libraries