Skip to content

Compiling GTest

cnoon edited this page Jan 25, 2013 · 2 revisions

Compiling GTest is quite a bit of work on any operating system. Find the appropriate one for you and make sure to follow the directions to a T.

Mac OS X

Compiling GTest on OS X is a huge pain. These instructions explain how to compile it using Xcode.

Compiling GTest as Static (*.a's)

  1. First download GTest-1.6.0
  2. Unzip the archive to $GTEST_HOME
  • A good example of $GTEST_HOME is /Users/username/Programming/Libraries/gtest-1.6.0-src
  1. Open $GTEST_HOME/xcode/gtest.xcodeproj
  2. Select the gtest project
  • Under Build Settings, make sure you have the 10.8 Base SDK selected
  • Under Build Settings, make sure you have the Apple LLVM compiler 4.1 selected
  1. Select the gtest-framework target
  • Under Build Settings, make sure you have the "Symbols Hidden by Default" set to "No" (removes visibility errors when compiling the Bump unittests)
  1. Select the gtest-static target
  • Under Build Settings, make sure you have the "Symbols Hidden by Default" set to "No"
  1. Select the gtest_main-static target
  • Under Build Settings, make sure you have the "Symbols Hidden by Default" set to "No"
  1. Select the Check target in the top-left dropdown to build
  2. Edit the scheme to build in Release
  3. Build the target (the samples and tests fail, but the framework and libraries succeed)
  4. Open a Finder window
  5. cd to ~/Library/Developer/Xcode/DerivedData/gtest-*/Build/Products/Release
  6. Copy the static libs to $INSTALL_HOME/lib
  • A good example of $GTEST_HOME is /Users/username/Programming/Libraries/gtest-1.6.0
  1. Edit the scheme to build in Debug
  2. Build the target (the samples and tests fail, but the framework and libraries succeed)
  3. Open a Finder window
  4. cd to ~/Library/Developer/Xcode/DerivedData/gtest-*/Build/Products/Debug
  5. Rename the gtest-static.a to gtest-staticd.a and the gtest_main-static.a to gtest_main-staticd.a
  6. Copy the Debug static libs to $INSTALL_HOME/lib
  7. Copy the $GTEST_HOME/include directory to $INSTALL_HOME/include

As you can see, this methods uses the static gtest libs and not the framework. If you want to use the framework, then you'll have to figure it out on your own :-(

Windows

Compiling GTest in Visual Studio isn't too bad. You want to use CMake to make it the easiest. The hardest decision is really whether to build static or shared libs.

Compiling GTest as Static (*.libs) - Preferred Method

  1. First download GTest-1.6.0
  2. Unzip the archive to $GTEST_HOME
  • A good example of $GTEST_HOME is C:\libraries\gtest-1.6.0-src
  1. Launch CMake (cmake-gui)
  2. Set the "Where is the source code:" to $GTEST_HOME
  3. Set the "Where to build the binaries:" to $GTEST_HOME\build.win32
  4. Hit the Configure button
  5. Select "Visual Studio 2008 9" and native default compilers
  6. Check both the Grouped and Advanced options
  7. Under Ungrouped Entries:
  • BUILD_SHARED_LIBS - Unchecked
  1. Under CMake:
  • CMAKE_CONFIGURATION_TYPES - Debug;Release
  • CMAKE_INSTALL_PREFIX - $INSTALL_HOME (C:\libraries\gtest-1.6.0)
  1. Under gtest:
  • gtest_build_samples - Unchecked
  • gtest_build_tests - Unchecked
  • gtest_disable_pthreads - Unchecked
  • gtest_force_shared_crt - Checked
  1. Hit the Configure button again (keep hitting under all red goes away)
  2. Hit the Generate button
  3. Go the the directory $GTEST_HOME\build.win32
  4. Open the ALL_BUILD.vcproj with Visual Studio
  5. Set the build type to Debug
  6. Right click on the ALL_BUILD project and select Build
  7. Set the build type to Release
  8. Right click on the ALL_BUILD project and select Build
  9. Go to the $GTEST_HOME\build.win32\Debug folder
  • Delete everything except the *.lib and *.pdb files
  • Add a "d" to the end of all the files
  • Create the $INSTALL_HOME\lib directory
  • Copy all the $GTEST_HOME\build.win32\Debug*.lib and *.pdb files to $INSTALL_HOME\lib
  1. Go to the $GTEST_HOME\build.win32\Release folder
  • Delete everything except the *.lib files
  • Copy all the $GTEST_HOME\build.win32\Debug*.lib files to $INSTALL_HOME\lib
  1. Copy the $GTEST_HOME\include folder to $INSTALL_HOME\include

Compiling GTest as Shared (*.libs and *.dlls)

  1. First download GTest-1.6.0
  2. Unzip the archive to $GTEST_HOME
  • A good example of $GTEST_HOME is C:\libraries\gtest-1.6.0-src
  1. Launch CMake (cmake-gui)
  2. Set the "Where is the source code:" to $GTEST_HOME
  3. Set the "Where to build the binaries:" to $GTEST_HOME\build.win32
  4. Hit the Configure button
  5. Select "Visual Studio 2008 9" and native default compilers
  6. Check both the Grouped and Advanced options
  7. Under Ungrouped Entries:
  • BUILD_SHARED_LIBS - Checked
  1. Under CMake:
  • CMAKE_CONFIGURATION_TYPES - Debug;Release
  • CMAKE_INSTALL_PREFIX - $INSTALL_HOME (C:\libraries\gtest-1.6.0)
  1. Under gtest:
  • Uncheck everything
  1. Hit the Configure button again (keep hitting under all red goes away)
  2. Hit the Generate button
  3. Go the the directory $GTEST_HOME\build.win32
  4. Open the ALL_BUILD.vcproj with Visual Studio
  5. Set the build type to Debug
  6. Right click on the ALL_BUILD project and select Build
  7. Set the build type to Release
  8. Right click on the ALL_BUILD project and select Build
  9. Go to the $GTEST_HOME\build.win32\Debug folder
  • Delete everything except the *.dll, *.lib and *.pdb files
  • Add a "d" to the end of all the files
  • Create the $INSTALL_HOME\lib and $INSTALL_HOME\bin directories
  • Copy all the $GTEST_HOME\build.win32\Debug*.dll files to $INSTALL_HOME\bin
  • Copy all the $GTEST_HOME\build.win32\Debug*.lib and *.pdb files to $INSTALL_HOME\lib
  1. Go to the $GTEST_HOME\build.win32\Release folder
  • Delete everything except the *.dll and *.lib files
  • Copy all the $GTEST_HOME\build.win32\Debug*.dll files to $INSTALL_HOME\bin
  • Copy all the $GTEST_HOME\build.win32\Debug*.lib files to $INSTALL_HOME\lib
  1. Copy the $GTEST_HOME\include folder to $INSTALL_HOME\include
  2. Add the $INSTALL_HOME\bin to the $PATH environment variable to pick up the dlls at runtime

It doesn't matter whether you use static or shared GTest libraries...totally your preference. I personally prefer to use static libraries to avoid having to set the $PATH environment variable. Plus it's just easier....less files to deal with.

Linux

Compiling GTest on linux isn't bad. You want to use CMake to make it the easiest.

Compiling GTest as Static (*.a's) - Preferred Method

  1. First download GTest-1.6.0
  2. Unzip the archive to $GTEST_HOME
  • A good example of $GTEST_HOME is /home/username/Programming/Libraries/gtest-1.6.0-src
  1. Launch CMake (cmake-gui)
  2. Set the "Where is the source code:" to $GTEST_HOME
  3. Set the "Where to build the binaries:" to $GTEST_HOME/build.linux64
  4. Hit the Configure button
  5. Select "Unix Makefiles" and native default compilers
  6. Check both the Grouped and Advanced options
  7. Under Ungrouped Entries:
  • BUILD_SHARED_LIBS - Unchecked
  1. Under CMake:
  • CMAKE_CONFIGURATION_TYPES - Debug
  1. Under gtest:
  • gtest_build_samples - Unchecked
  • gtest_build_tests - Unchecked
  • gtest_disable_pthreads - Unchecked
  • gtest_force_shared_crt - Checked
  1. Hit the Configure button again (keep hitting under all red goes away)
  2. Hit the Generate button
  3. Go to the directory $GTEST_HOME/build.linux64
  4. Type "make -j 8"
  5. Rename libgtest.a to libgtestd.a
  6. Rename libgtest_main.a to libgtest_maind.a
  7. Create the $INSTALL_HOME/lib directory
  • A good example of $INSTALL_HOME is /home/username/Programming/Libraries/gtest-1.6.0
  1. Copy libgtestd.a and libgtest_maind.a to $INSTALL_HOME/lib
  2. Launch CMake (cmake-gui)
  3. Under CMake:
  • CMAKE_CONFIGURATION_TYPES - Release
  1. Hit the Configure button again (keep hitting under all red goes away)
  2. Hit the Generate button
  3. Go to the directory $GTEST_HOME/build.linux64
  4. Type "make -j 8"
  5. Copy libgtest.a and libgtest_main.a to $INSTALL_HOME/lib
  6. Copy the $GTEST_HOME/include folder to $INSTALL_HOME/include

It doesn't matter whether you use static or shared GTest libraries...totally your preference. If you want to use shared libraries, then slightly alter the CMake settings and make sure to set your LD_LIBRARY_PATH environment variable. I personally prefer to use static libraries to avoid having to set more environment variables.

Clone this wiki locally