Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Describes how to install and run GTest, a Google framework to conduct unit testing in C++

Build and install Google Test

Download the latest (1.7.0) from Google Code (Q: where is it going to live, once GCode shuts down?)

Then follow the primer, but more to the point, the README (YMMV) Having installed CLion and cmake, this is how I built gtest:

brew install cmake
cd gtest-1.7.0
mkdir build
cd build
cmake -Dgtest_build_samples=ON -Dgtest_build_tests=ON /Users/marco/Downloads/gtest-1.7.0
make
make test
./sample1_unittest

NOTE you will need to install cmake on your Mac, I used CLion distribution in:

/Applications/CLion.app/Contents/bin/cmake/bin/cmake

Once this all works, you can "install" it, by putting the generated .a files in a place that can be easily found.

On a Mac OSX, I used the following:

sudo mkdir /usr/local/Cellar/gtest
sudo cp gtest-1.7.0/build/libgtest.a /usr/local/Cellar/gtest/
sudo ln -snf /usr/local/Cellar/gtest/libgtest.a /usr/local/lib/libgtest.a
sudo cp -r gtest-1.7.0/include /usr/local/Cellar/gtest/
ln -snf ../Cellar/gtest/include/gtest /usr/local/include/gtest

You will then need to make it so that gcc can find the includes and the libgtest - for example in CMakeLists.txt add the following:

include_directories(/usr/local/include)
target_link_libraries(test_inet /usr/local/lib/libgtest.a)

It currently appears that CLion gets mightly confused and can't find the includes, but it all compiles and run just fine (see CPP2932).

Useful Links

@straightjacket

This comment has been minimized.

Show comment Hide comment
@straightjacket

straightjacket Feb 22, 2017

Thank you! This solved my problem straightaway!

Thank you! This solved my problem straightaway!

@csukuangfj

This comment has been minimized.

Show comment Hide comment
@csukuangfj

csukuangfj Apr 16, 2017

Informative!

To specify the installation location instead of using the default location, use -D CMAKE_INSTALL_PREFIX=/path/to/installation/directory

Informative!

To specify the installation location instead of using the default location, use -D CMAKE_INSTALL_PREFIX=/path/to/installation/directory

@alexperrone

This comment has been minimized.

Show comment Hide comment
@alexperrone

alexperrone Jul 19, 2017

You might consider replacing the relative path ln -snf ../Cellar/gtest/include/gtest /usr/local/include/gtest by changing to ln -snf /usr/local/Cellar/gtest/include/gtest /usr/local/include/gtest

You might consider replacing the relative path ln -snf ../Cellar/gtest/include/gtest /usr/local/include/gtest by changing to ln -snf /usr/local/Cellar/gtest/include/gtest /usr/local/include/gtest

@guanhuamai

This comment has been minimized.

Show comment Hide comment
@guanhuamai

guanhuamai Jul 30, 2017

helps a lot.
alexperrone. That's true

helps a lot.
alexperrone. That's true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment