Skip to content

Instantly share code, notes, and snippets.

@butuzov
Last active April 4, 2024 10:08
Show Gist options
  • Star 29 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save butuzov/e7df782c31171f9563057871d0ae444a to your computer and use it in GitHub Desktop.
Save butuzov/e7df782c31171f9563057871d0ae444a to your computer and use it in GitHub Desktop.
Bash Script to install Google Test and Google Mock on Mac

Installing Google Test on Mac

One bash script to install google test (and mock) on your Mac. I assume you already had install cmake (if not, use brew - brew install cmake).

It use brew directories to store gmock and gtest.

gtest_installer.sh

#!/usr/bin/env bash

# Current directory
__THIS_DIR=$(pwd)


# Downloads the 1.8.0 to disc
function dl {
    printf "\n  Downloading Google Test Archive\n\n"
    curl -LO https://github.com/google/googletest/archive/release-1.8.0.tar.gz
    tar xf release-1.8.0.tar.gz
}

# Unpack and Build
function build {
    printf "\n  Building GTest and Gmock\n\n"
    cd googletest-release-1.8.0
    mkdir build 
    cd $_
    cmake -Dgtest_build_samples=OFF -Dgtest_build_tests=OFF ../
    make
}

# Install header files and library
function install {
    printf "\n  Installing GTest and Gmock\n\n"

    USR_LOCAL_INC="/usr/local/include"
    GTEST_DIR="/usr/local/Cellar/gtest/"
    GMOCK_DIR="/usr/local/Cellar/gmock/"

    mkdir $GTEST_DIR

    cp googlemock/gtest/*.a $GTEST_DIR
    cp -r ../googletest/include/gtest/  $GTEST_DIR
    ln -snf $GTEST_DIR $USR_LOCAL_INC/gtest
    ln -snf $USR_LOCAL_INC/gtest/libgtest.a /usr/local/lib/libgtest.a
    ln -snf $USR_LOCAL_INC/gtest/libgtest_main.a /usr/local/lib/libgtest_main.a

    mkdir $GMOCK_DIR
    cp googlemock/*.a   $GMOCK_DIR
    cp -r ../googlemock/include/gmock/  $GMOCK_DIR
    ln -snf $GMOCK_DIR $USR_LOCAL_INC/gmock
    ln -snf $USR_LOCAL_INC/gmock/libgmock.a /usr/local/lib/libgmock.a
    ln -snf $USR_LOCAL_INC/gmock/libgmock_main.a /usr/local/lib/libgmock_main.a
}

# Final Clean up.
function cleanup {
    printf "\n  Running Cleanup\n\n"

    cd $__THIS_DIR
    rm -rf $(pwd)/googletest-release-1.8.0
    unlink $(pwd)/release-1.8.0.tar.gz
}

dl && build && install && cleanup 

Usage

chmod +x ./gtest_installer.sh
sudo ./gtest_installer.sh

Test

#include "gtest/gtest.h"

TEST( ExampleTest, First ) {
  EXPECT_TRUE(  true );
}

int main(int argc, char **argv) {
  ::testing::InitGoogleTest(&argc, argv); 
  return RUN_ALL_TESTS();
}
clang++ -lgtest test.cpp -o  ./test -std=c++14 && ./test
@loeksnokes
Copy link

Slick... Thank you!

@jeffrey1hu
Copy link

Thank you!!

@wangzhangjun
Copy link

cool

@SumitkumarSatpute
Copy link

Permission denied for coping into the /usr/* location

@nickbarratt
Copy link

Very good. Thanks for posting this!

@kayala
Copy link

kayala commented Sep 10, 2021

Thank you!

@misterprog-dev
Copy link

Thanks for this sharing, it was a helpfull

@chavychaze
Copy link

Thank you!

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