How to install GTest on Mac OS X with homebrew?
Asked Answered
G

9

36

I'm trying to install gtest with my packet manager Home Brew but there is no repository for it. I tried to download gtest from code.google but I can't understand how to install it, because cmake and make don't solve the problem.

Grapher answered 6/4, 2013 at 15:21 Comment(1)
it is now possible: brew install googletestHanuman
M
41

If you want the latest version without using Homebrew:

git clone https://github.com/google/googletest
cd googletest
mkdir build
cd build
cmake ..
make
make install
Magnific answered 6/10, 2017 at 17:59 Comment(6)
Might sound dumb, but can you delete the git repo after the installation ?Bilingual
@MathieuChâteauvert you can. The last command "make install" compiles the the project and installs the output files in your system library path so removing the source code afterward won't change anything.Goldenrod
works everything except make install that gives No rule to make target 'install'Stricklan
It is much better to install the tag's under the released section of the repository. Doing the above will cause errors if the master branch is not fully correct! For me, version v1.8.1 worked. Download the tar file, extract, then use from line 3 to end on the above answer.Tound
If make fails.. try cmake .. -DCMAKE_CXX_STANDARD=17 then make. Referred from: github.com/google/googletest/issues/1519#issuecomment-593001952Dionisio
Can confirm that this works beautifully on the M1 mac. The last command will require sudo.Adenine
N
16

I just installed gtest using cmake and make. I can show you how did I install Google Test manually. It's not complicated, just following the steps.

  1. download the gtest file from https://github.com/google/googletest/archive/release-1.8.0.zip and unzip it.
cd googletest-release-1.8.0/googletest/
mkdir bld
cd bld
cmake -DCMAKE_CXX_FLAGS=-std=c++11 ..
make
cp -a ../include/gtest /usr/local/include
cp -a *.a /usr/local/lib
  1. delete the folder googletest-release-1.8.0 and release-1.8.0.zip

If you want to use google-test please use #include <gtest/gtest.h>.

If you want to use Google Test version 1.7.0, just download the release-1.7.0.zip and in the 2. step use cd googletest-release-1.7.0 instead of cd googletest-release-1.8.0/googletest/. The rest steps are the same.

I added the flag -DCMAKE_CXX_FLAGS=-std=c++11 to use cmake with c++11.

Enjoy it!


Updated version

Start from v1.8.0 gmock is included in the release file.

Here is the general steps for installing gtest and gmock in v1.8.0 - v1.10.0. (Feel free to replace 1.10.0 with your desired version.)

# download release file and extract files from it
curl -LOk https://github.com/google/googletest/archive/release-1.10.0.tar.gz
tar -zxvf release-1.10.0.tar.gz
rm release-1.10.0.tar.gz

cd googletest-release-1.10.0/
mkdir build
cd build
# build gtest and gmock
cmake -DCMAKE_CXX_FLAGS=-std=c++11 ..
make

# copy requisite c++ files and compiled files to correct directories
cp -a ../googletest/include/* /usr/local/include
cp -a ../googlemock/include/* /usr/local/include
find . -name "*.a" -exec cp -a {} /usr/local/lib \;

# clean
cd ../..
rm -r googletest-release-1.10.0

If you want to install only gtest not gmock, you can do the following:

  1. remove the command cp -a ../googlemock/include/ /usr/local/include

  2. replace

     find . -name "*.a" -exec cp -a {} /usr/local/lib \;
    

    with

     find . -name "libgtest*.a" -exec cp -a {} /usr/local/lib \;`
    
Nationwide answered 9/3, 2017 at 23:27 Comment(1)
This is the more correct answer! master has issues from time to time and it is better to install the released versions.Tound
J
7

For the question 'Why there is no repository for it?' see related gtest FAQ question. But you can create formula by yourself if you want - see this post for the details (but don't sure if it will work for 1.6).

But I suggest you just install gtest: read the readme for the detailed instructions. There are few simple steps:
Download and extract sources to some directory gtest_dir.
Build object files:

g++ -I$gtest_dir/include -I$gtest_dir -c $gtest_dir/src/gtest-all.cc
g++ -I$gtest_dir/include -I$gtest_dir -c $gtest_dir/src/gtest_main.cc

Link:

ar -rv libgtest.a gtest-all.o
ar -rv libgtest_main.a gtest_main.o


Note: if you want to build gtest with support of C++11 and libc++ you need to do some extra work:

Jurgen answered 8/4, 2013 at 6:40 Comment(1)
It's annoying because the included Makefile (as of 2/4/16) creates a library for gtest_main, but not for gtest-all. Plus, most of the tutorials refer to the name libgtest, which doesn't seem to exist unless you specify manually, as in your answer. Thanks for the tips!Wilford
S
7

You can use this:

brew install --HEAD https://gist.githubusercontent.com/Kronuz/96ac10fbd8472eb1e7566d740c4034f8/raw/gtest.rb
Sandpaper answered 11/3, 2018 at 17:55 Comment(0)
B
7

Homebrew formula: brew install googletest

Banta answered 8/2, 2021 at 7:24 Comment(0)
S
6

This is an alternative solution for slund's answer. cmake .. didn't work for me.

cd desktop
git clone https://github.com/google/googletest.git
cd googletest
mkdir build
cd build
cmake -DCMAKE_CXX_COMPILER="c++" -DCMAKE_CXX_FLAGS="-std=c++11 -stdlib=libc++" ../
make
sudo make install
Shang answered 5/3, 2020 at 12:24 Comment(1)
This is the command that worked for me on both Mac OS and Ubuntu. On Ubuntu I got an error "unrecognized stdlib" which deleting -stdlib=libc++ solved the problem.Hamon
H
1

The ROS repo (http://wiki.ros.org/kinetic/Installation/OSX/Homebrew/Source) has gtest:

brew tap ros/deps
brew instal gtest
Hardening answered 4/2, 2020 at 22:13 Comment(0)
T
1

Latest Homebrew does no longer support installation from GitHub Gists, so @Kronuz's solution no longer works. Here's the error:

Error: Calling Non-checksummed download of gtest formula file from an arbitrary URL is disabled! Use 'brew extract' or 'brew create' and 'brew tap-new' to create a formula file in a tap on GitHub instead.

GoogleTest can now be installed with the following command:

brew install web-eid/gtest/gtest

The underlying repository is based on @Kronuz's gist, thanks @Kronuz's for creating the gist!

Troopship answered 9/11, 2020 at 20:5 Comment(0)
S
0

If you want to install it from sources the simplest way is as follows

git clone https://github.com/google/googletest
cd googletest
cmake . -DCMAKE_BUILD_TYPE=Release 
make install
Soilure answered 20/12, 2021 at 17:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.