How to properly setup googleTest on OS X aside from XCode

11,679

Solution 1

Before you start make sure your have read and understood this note from Google! This tutorial makes using gtest easy, but may introduce nasty bugs.

1. Get the googletest framework

$ wget https://github.com/google/googletest/archive/release-1.8.0.zip

Or get it by hand. I guess I won't manitain this little How-to, so if you stumbled upon it and the links are outdated, feel free to edit it.

2. Unzip and build google test

$ unzip gtest-1.8.0.zip
$ cd gtest-1.8.0
$ ./configure
$ make

3. "Install" the headers and libs on your system.

$ sudo cp -a include/gtest /usr/include
$ sudo cp -a lib/.libs/* /usr/lib/

gTestframework is now ready to use. Just don't forget to link your project against the library by setting -lgtest as linker flag and optionally, if you did not write your own test mainroutine, the explicit -lgtest_main flag.

From here on you might want to go to Googles documentation about the framework to learn how it works. Happy coding!

Solution 2

It's adviced that you link statically. There's no secret. Being a bit offtopic, I use CMake in my projects, which I recommend, and here (https://github.com/oblitum/operations) I have setup a very basic skeleton project that links to gmock and gtest (it's also adviced by google that you use the same gtest from gmock, when you use gmock). In the external folder reside the external CMake files that actually import gtest and gmock through ExternalProject_Add. In the sample, I'm setting the URL as a file path in my system where gmock and gtest are downloaded, but, if you check CMake ExternalProject_Add docs you can see that download urls, online repository urls are also available, which can allow your build to download gtest and gmock, and cache it, automatically.

Solution 3

I think cmake is an easy way to setup and use gtest on OSX. It works without manually copying files. Unzip gooletest-release-1.8.0, then

cd googletest-release-1.8.0

# create a build directory
mkdir build      
cd build

# build configuration
cmake .. -DBUILD_GTEST=ON -DBUILD_SHARED_LIBS=ON

# build it 
make   

# installation
sudo make install

Afterwards, you can easily incorporate gtest in your project with the cmake commands

# sets GTEST_INCLUDE_DIRS and GTEST_LIBRARIES
find_package( GTest REQUIRED )      

# adds the gtest include directory
include_directories( ${GTEST_INCLUDE_DIRS} )

# links gtest
target_link_libraries( yourTestApp ${GTEST_LIBRARIES} )
Share:
11,679

Related videos on Youtube

ManuelSchneid3r
Author by

ManuelSchneid3r

Updated on October 31, 2022

Comments

  • ManuelSchneid3r
    ManuelSchneid3r over 1 year

    How do I setup gTest, so that I can link aganist the library? I will code in vim, so I just want to install the libraries, unlike the XCode setup. Goal is to be able to link a project against the library by setting -lgtest as linker flag and optionally, if I did not write my own test mainroutine, the explicit -lgtest_main flag.

  • Alessandro Pezzato
    Alessandro Pezzato over 8 years
    if you code in vim, you'll find useful this plugin: vim-gtest
  • BuroBernd
    BuroBernd over 7 years
    on the latest OSX, at step 3, i get: "operation not permitted". i seem to be unable to create any folders at /user/include. what can i do?
  • human torch
    human torch over 7 years
    put it in /usr/local/include
  • Aman Tandon
    Aman Tandon about 7 years
    this tutorial is not working anymore ....... ./configure -bash: ./configure: No such file or directory
  • ManuelSchneid3r
    ManuelSchneid3r about 7 years
    Looks like they switched to cmake. Run cmake .