Hack w/ Limbic Media

Interactive Electronica and IoT

Visit Limbic Media

hack.limbicmedia.ca was created by the developers and engineers at Limbic Media to share their knowledge and experiences with DIY and hacker community-at-large.

by Steven Bjornson

Installing Google Test (on OS X)

While in my previous post I boasted about the ease of using Google Test, I will admit it did take some effort to get working for my system. I can say, this is likely only because I didn't want to use XCode.

Here are some system details:

  • OS: OS X, 10.9.5

  • shell: zsh, via oh my zsh

  • compiler: g++, although gcc should work for every step

Below is a step by step showing how I got it all together for my system.

Warning: this is not meant to be exhaustive guide but instead will hopefully get some people past the installation confusion I had.

So, open up terminal and start C/P-ing:

Step 1: get the git

I cloned the GitHub repository right into ~/ but you can put it anywhere you want:

cd ~/  
git clone https://github.com/google/googletest.git  
Step 2: cmake

cmake is a tool which assesses your system in order to setup the parameters necessary for compiling and installing c/c++ programs/libraries.

Note: Installing cmake is not within the context of this tutorial but homebrew makes it easy.

We make a new folder inside of the Google Test git directory

cd ~/googletest/ #change to Google Test directory  
mkdir install #make new directory  
cd install #change to new directory  

next, we run cmake with a couple flags

cmake -DCMAKE_CXX_COMPILER="c++" -DCMAKE_CXX_FLAGS="-std=c++11 -stdlib=libc++" ../  

-DCMAKE_CXX_COMPILER="c++" changes the default compiler to g++ and -DCMAKE_CXX_FLAGS="-std=c++11 -stdlib=libc++" sets the compiler to C++11 and the standard library to libc++

Step 3: make

Not to be confused with cmake, this step takes the make file created in the last step (basically a set of instructions for compiling the code) and uses it to compile and install the Google Test code.

make #compiles the code  
sudo make install #installs the code  

Note: the sudo line will prompt for a password.

Step 4: setting path variables

Step 3 creates a shared library but my system wasn't setup to know where that was.

I use zsh and so the next set of commands is for that shell. If your terminal is standard bash, in the commands below replace .zshrc with .bash_profile and it should work:

echo "export CPLUS_INCLUDE_PATH=/usr/local/include" >> ~/.zshrc  
echo "export LIBRARY_PATH=/usr/local/lib" >> ~/.zshrc  
source ~/.zshrc #tell terminal there are new variables  

The above commands create two environment variables (CPLUS_INCLUDE_PATH and LIBRARY_PATH). The first points to the location where all the system libraries are installed, this is where the header files for Google Test are. The second points to the location that all the shared libraries are installed on OS X.

During step 3, parts of Google Test was installed between these two directories.

Step 5: running your first test

This is the payoff.

First, we'll make a new directory and copy some files into it from the Google Test directory.

cd ~/  
mkdir my_test  
cd my_test  
cp ~/googletest/googletest/samples/sample1.cc  
cp ~/googletest/googletest/samples/sample1.h  
cp ~/googletest/googletest/samples/sample1_unittest.cc  
cp ~/googletest/googletest/src/gtest_main.cc ./  

The first three files are for the first sample test included with Google Test.

The fourth file is a sample main file which will run all the tests (in this case, just one). In the future you can copy this file (or even use it at its original location) for running your tests.

Next, we compile all the necessary files:

g++ -std=c++11 -stdlib=libc++  sample1_unittest.cc sample1.cc gtest_main.cc -lgtest -lpthread -o sample1  

Important: this is the command you will use to compile your future tests. When that day comes, remember to change the .cc files to match those of the code you've written.

Above you will see all of the .cc file which were copied over. This step is compiling all of these files and will turn them into an executable.

You will also see two of the flags that we set during step 2.
Note: these flags are only necessary because I want to ensure i'm using C++11.

Additionally, you will see two flags for the linker. These flags beginning with -l and are for shared libraries. Without setting the LIBRARY_PATH in step 4, it is possible your system won't know where these are.

The last flag -o tells the compiler to call the output file sample1.

Now, as long as the compiler and linker were error free, the test can be run:

./sample1

The output of the test should print to the terminal screen.

Conclusion

If everything worked in from these steps you should now be able to write your own tests.

Take a look at sample1_unittest.cc and the Google Test Primer to see how to structure your tests.

Need help with a similar project?

Innovative Digital Art

Limbic Media

Consulting Engineering

Limbic Consulting