Getting started with Google Test (GTest) on Ubuntu

Google test is a framework for writing C++ unit tests. In this short post, I explain how to set it up in Ubuntu.

Start by installing the gtest development package:

sudo apt-get install libgtest-dev

Note that this package only install source files. You have to compile the code yourself to create the necessary library files. These source files should be located at /usr/src/gtest. Browse to this folder and use cmake to compile the library:

sudo apt-get install cmake # install cmake
cd /usr/src/gtest
sudo cmake CMakeLists.txt
sudo make
# copy or symlink libgtest.a and libgtest_main.a to your /usr/lib folder
sudo cp *.a /usr/lib

Lets say we now want to test the following simple squareRoot function:

// whattotest.cpp
#include <math.h>
double squareRoot(const double a) {
    double b = sqrt(a);
    if(b != b) { // nan check
        return -1.0;
        return sqrt(a);

In the following code, we create two tests that test the function using a simple assertion. There exists many other assertion macros in the framework (see The code contains a small main function that will run all of the tests automatically. Nice and simple!

// tests.cpp
#include "whattotest.cpp"
#include <gtest/gtest.h>
TEST(SquareRootTest, PositiveNos) { 
    ASSERT_EQ(6, squareRoot(36.0));
    ASSERT_EQ(18.0, squareRoot(324.0));
    ASSERT_EQ(25.4, squareRoot(645.16));
    ASSERT_EQ(0, squareRoot(0.0));
TEST(SquareRootTest, NegativeNos) {
    ASSERT_EQ(-1.0, squareRoot(-15.0));
    ASSERT_EQ(-1.0, squareRoot(-0.2));
int main(int argc, char **argv) {
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();

The next step is to compile the code. I’ve set up a small CMakeLists.txt file below to compile the tests. This file locates the google test library and links it with the test application. Note that we also have to link to the pthread library or the application won’t compile.

cmake_minimum_required(VERSION 2.6)
# Locate GTest
find_package(GTest REQUIRED)
# Link runTests with what we want to test and the GTest and pthread library
add_executable(runTests tests.cpp)
target_link_libraries(runTests ${GTEST_LIBRARIES} pthread)

Compile and run the tests:

cmake CMakeLists.txt

Have fun testing! You can download all of the code above at my Github page:


You may also like...

75 Responses

  1. srinivas says:

    HI when I’m executing make command its Showing no rule to make target
    error .How to do it makefile Could you please explain anyone??

  2. abhi says:

    how to write gtest in cpp if there is no return value from the function.
    please show the sample for the same

  3. Pedro Fernandez Acuna says:

    Modern CMake suggests never using include_directories() as you are operating on the directory level. It’s much better operate just on the targets. See Youtube video (at:18:26) “C++Now 2017: Effective CMake” by Daniel Pfeifer

  4. Lin says:

    Hi, thanks for this quick guidance. However, I constantly meeting this same error:
    tests.cpp:(.text+0xf8): undefined reference to `testing::Message::Message()’
    anything I did wrong?

  5. Anonymous says:

    How to get html code coverage report for this tutorial…please help me

  6. Santiago H says:

    you can use “sudo make install” instead of copying the lib

  7. Laura says:

    How would this work on Linux Mint OS? I’ve been playing with it, and it doesn’t work the same.

    • Maxim says:

      It works on Ubuntu 16.04 if you clone the gtest repo, build it, and install the library manually
      git clone
      mkdir -p googletest/build
      cd googletest/build
      cmake ..
      sudo make install

      • Anitha says:

        Gtest is running in ubuntu 16.04 but not in ubuntu 18.04. Could you please tell what will be the issue.

        • Anitha says:

          [==========] Running 10 tests from 5 test cases.
          [———-] Global test environment set-up.
          [———-] 5 tests from AgentAppTest
          [ RUN ] AgentAppTest.test_getInstance

          When 1st test case encountered it will stop running.

  8. hassin ayaz says:

    thanks a lot

  9. Add gtest_main to the target_link_libraries so you can remove your main method in the test code. Like so:

    `target_link_libraries(runTests ${GTEST_LIBRARIES} gtest_main pthread)`

  10. Some guy says:

    Very helpful guide and compensates for Google’s near universal-inability to provide decent docs or getting started guides for its open source projects. Thanks very much for this!

  11. Anonymous says:


  12. ranjon says:

    compilation errors have occurred while I build the FAST library on ubuntu.

    /home/kerax/Desktop/FAST/src/source/CL/cl.hpp:4755:28: warning: ignoring attributes on template argument ‘cl_int {aka int}’ [-Wignored-attributes]
    VECTOR_CLASS* binaryStatus = NULL,
    In file included from /home/kerax/Desktop/FAST/src/source/CL/OpenCL.hpp:5:0,
    from /home/kerax/Desktop/FAST/src/source/FAST/RuntimeMeasurementManager.hpp:6,
    from /home/kerax/Desktop/FAST/src/source/FAST/ExecutionDevice.hpp:5,
    from /home/kerax/Desktop/FAST/src/source/FAST/Data/DataObject.hpp:6,
    from /home/kerax/Desktop/FAST/src/source/FAST/AffineTransformation.hpp:4,
    from /home/kerax/Desktop/FAST/src/source/FAST/Visualization/View.hpp:5,
    from /home/kerax/Desktop/FAST/src/source/FAST/Visualization/WindowWidget.hpp:13,
    from /home/kerax/Desktop/FAST/src/source/FAST/Visualization/WindowWidget.cpp:1:
    /home/kerax/Desktop/FAST/src/source/CL/cl.hpp:4755:28: warning: ignoring attributes on template argument ‘cl_int {aka int}’ [-Wignored-attributes]
    VECTOR_CLASS* binaryStatus = NULL,
    CMakeFiles/Makefile2:187: recipe for target ‘CMakeFiles/FAST.dir/all’ failed
    make[1]: *** [CMakeFiles/FAST.dir/all] Error 2
    Makefile:129: recipe for target ‘all’ failed
    make: *** [all] Error 2

  13. Anonymous says:

    Crazy. The ubuntu package is not even compiled…

  14. Anonymous says:

    How to add the gtest libraries using g++?

  15. Dov says:

    The tutorial for gtest is good, but the example isn’t well chosen. It’s not a good idea to return -1 for bad values, so why give that as an example? Better to show that you expect the function to return NaN for negative numbers, and infinity for positive numbers. That would actually be useful.

    If you want to write your own function, do hypotenuse rather than messing with a square root function that works!

  16. jeho park says:

    great thanks !! it’s was very useful

  17. Searene says:

    That’s really helpful, thanks!

  18. Anonymous says:

    Great tutorial

  19. Anonymous says:

    It works perfectly on Raspberry Pi 3 as well. Thank you so much 🙂

  20. Jules says:

    If I separate the main test from the code, it does not execute the tests and says 0 test executed. Any idea?.


    • Erik Smistad says:

      If you want to split the tests over multiple files you need to add the files to the line add_executable(runTests tests.cpp newFile.cpp) in CMakeLists.txt

  21. Anonymous says:

    very helpful, thanks

  22. Anonymous says:

    Very helpful and effective. Thanks a lot.

  23. Paul Campbell says:

    Thank You. I had struggled months ago trying to get going with GoogleTest.
    Your example gave me reassurance that the install went correctly.
    I used it on Centos 7
    # s/apt-get/yum
    Again thanks for helping some many others.

  24. Start says:

    Thanks !

  25. cat&ant says:

    Hello Erik,
    I don’t find the gmock after following your setup instructions here(by installing libgtest-dev). How can I add gmock seamlessly and run successfully? I tried but I am keeping getting some error. So I have to uninstall libgtest-dev.

  26. cat&ant says:

    Great. Thanks a lot for this helpful info!!

  27. Bharath says:

    Thanks…It helped me a lot.

  28. Adrian Schneider says:

    Thank you!

  29. Anonymous says:

    Thanks very much..

  30. Prabeen says:

    Hi ..
    I fallow the same steps but it was given error like .

    /usr/include/c++/4.6/bits/stl_bvector.h:871:45: error: ‘((std::vector<bool, std::allocator >*)this)->std::vector<bool, std::allocator >::.std::_Bvector_base<std::allocator >::_M_allocate’ cannot be used as a function
    /usr/include/c++/4.6/bits/stl_bvector.h:876:7: error: ‘difference_type’ was not declared in this scope
    make[2]: *** [CMakeFiles/runTests.dir/tests.cpp.o] Error 1
    make[1]: *** [CMakeFiles/runTests.dir/all] Error 2
    make: *** [all] Error 2

    Please some one help to resolve this.

  31. Vic says:

    Worked out of the box! Nice! What about Google Mock? Any instructions for that? I understand it’s now under the Test fold. Lastly, I did this instead:
    mkdir /usr/local/lib/gtest
    cp *.a /usr/local/lib/gtest
    ln -s /usr/local/lib/gtest/libgtest.a /usr/lib/libgtest.a
    ln -s /usr/local/lib/gtest/libgtest_main.a /usr/lib/libgtest_main.a
    That way, the libs are contained in my local lib.

  32. Paweł says:

    How to create link to the pthread library on ubuntu?

    • Erik Smistad says:

      That is what the last line in the cmake file does: target_link_libraries(runTests ${GTEST_LIBRARIES} pthread)

      If you are compiling directly with gcc you can simply add -lpthread

  33. Han Qiu says:

    Thanks! Solved my problem~

  34. vptarmigan says:

    Great guide for beginners, thank you!

  35. Patrick says:

    Thanks Erik, helped me get started using gTest for my project.

  36. Anonymous says:

    Really clear, simple, short guide that did exactly what I wanted. Thank you!

  37. Martin says:

    thanks for this guide. It is working now.

  38. Miguel B says:

    Thank you very much, this was very helpful!

  39. Quan Wang says:

    At the last step, when I type cmake CMakeLists.txt, I get these errors:

    CMake Error at /usr/share/cmake-2.8/Modules/FindPackageHandleStandardArgs.cmake:91 (MESSAGE):
    Could NOT find GTest (missing: GTEST_INCLUDE_DIR)
    Call Stack (most recent call first):
    /usr/share/cmake-2.8/Modules/FindPackageHandleStandardArgs.cmake:252 (_FPHSA_FAILURE_MESSAGE)
    /usr/share/cmake-2.8/Modules/FindGTest.cmake:150 (FIND_PACKAGE_HANDLE_STANDARD_ARGS)
    CMakeLists.txt:4 (find_package)

    — Configuring incomplete, errors occurred!

    Do you know how to solve this problem? Thanks.

  40. Deepakgiri says:

    my folder structure is as below :

    deepak@deepak:/usr/src/gtest$ ll
    total 1256
    drwxr-xr-x 6 root root 4096 Sep 18 18:29 ./
    drwxr-xr-x 7 root root 4096 Sep 18 18:03 ../
    drwxr-xr-x 2 root root 4096 Sep 18 18:03 cmake/
    -rw-r–r– 1 root root 11730 Sep 18 18:12 CMakeCache.txt
    drwxr-xr-x 7 root root 4096 Sep 18 18:12 CMakeFiles/
    -rw-r–r– 1 root root 1552 Sep 18 18:12 cmake_install.cmake
    -rw-r–r– 1 root root 8664 Apr 16 2011 CMakeLists.txt
    -rw-r–r– 1 root root 1218562 Sep 18 18:12 libgtest.a
    -rw-r–r– 1 root root 3726 Sep 18 18:12 libgtest_main.a
    -rw-r–r– 1 root root 5903 Sep 18 18:12 Makefile
    drwxr-xr-x 2 root root 4096 Sep 18 18:03 src/
    drwxr-xr-x 3 root root 4096 Sep 19 14:48 unittest/
    deepak@deepak:/usr/src/gtest$ cd unittest/
    deepak@deepak:/usr/src/gtest/unittest$ ll
    total 52
    drwxr-xr-x 3 root root 4096 Sep 19 14:48 ./
    drwxr-xr-x 6 root root 4096 Sep 18 18:29 ../
    -rw-r–r– 1 root root 12273 Sep 19 12:11 CMakeCache.txt
    drwxr-xr-x 9 root root 4096 Sep 19 14:48 CMakeFiles/
    -rw-r–r– 1 root root 1579 Sep 18 18:40 cmake_install.cmake
    -rw-r–r– 1 root root 296 Sep 19 14:48 CMakeLists.txt
    -rw-r–r– 1 root root 332 Sep 19 13:00 CMakeLists.txt~
    -rw-r–r– 1 root root 4574 Sep 19 14:48 Makefile
    -rw-r–r– 1 root root 499 Sep 19 14:22 tests.cpp
    -rw-r–r– 1 root root 175 Sep 18 18:33 whattotest.cpp

  41. Deepakgiri says:

    Hi All,

    I got some reference error while execute “make” command.
    Error :
    Linking CXX executable runTests
    CMakeFiles/runTests.dir/tests.cpp.o: In function `testing::AssertionResult testing::internal::CmpHelperEQ(char const*, char const*, int const&, double const&)’:
    tests.cpp:(.text._ZN7testing8internal11CmpHelperEQIidEENS_15AssertionResultEPKcS4_RKT_RKT0_[testing::AssertionResult testing::internal::CmpHelperEQ(char const*, char const*, int const&, double const&)]+0x9b): undefined reference to `testing::internal::EqFailure(char const*, char const*, testing::internal::String const&, testing::internal::String const&, bool)’
    CMakeFiles/runTests.dir/tests.cpp.o: In function `testing::AssertionResult testing::internal::CmpHelperEQ(char const*, char const*, double const&, double const&)’:
    tests.cpp:(.text._ZN7testing8internal11CmpHelperEQIddEENS_15AssertionResultEPKcS4_RKT_RKT0_[testing::AssertionResult testing::internal::CmpHelperEQ(char const*, char const*, double const&, double const&)]+0x99): undefined reference to `testing::internal::EqFailure(char const*, char const*, testing::internal::String const&, testing::internal::String const&, bool)’
    collect2: ld returned 1 exit status
    make[2]: *** [runTests] Error 1
    make[1]: *** [CMakeFiles/runTests.dir/all] Error 2
    make: *** [all] Error 2

    Please help me in this.

    Deepakgiri Aparnathi

  42. olupot says:

    Hey buddy, lovely Blog. Thanx for the good work. I love test driven design but was finding it rather hard to implement in C++ because of the libararies. Thanx alot.

  43. David says:

    You’ve written that to run it:

    cmake CMakeLists.txt

    It works, but then a lot of files has been created. Is it possible to run it in another way, e.g. I have got some ode to test I am writing some testSuite for this and then to run it I type, e.g:

    g++ -googleTest testSuite.cpp -o testSuite


    • Erik Smistad says:

      cmake creates a lot of files. If you don’t like to have all of those files in your source folder you can always create a separate (build) folder for this.

      For instance:

      mkdir build
      cd build
      cmake ../

  44. Ramesh says:


  45. Chetan says:

    If you get lots of errors after executing

    sudo cmake CMakeLists.txt

    update your libraries, by executing :

    sudo apt-get build-essentials

  46. Thanks. Very nice and simple tut.

  47. Anonymous says:

    I’m so thank, it helps me alot

  48. Venkat says:

    Thanks, It hedlped me how to get started….

  49. Janne Pohjoinen says:

    That was helpful, thanks!

  50. jules says:

    Hey mate,

    thanks alot! As already mentioned short but to the point and very informative…

  51. piponazo says:

    Very helpful! I used to install the libgtest-dev package and then to compile the libraries from the tarball downloaded from the google-test webpage. I didn’t know that the ubuntu package install those src files.

    I would like to suggest to compile the libraries as shared ones using the following command for configuring the project:

    sudo cmake -D BUILD_SHARED_LIBS=ON ..

    I commonly prefer to use shared libraries in front of static ones.


  52. simon says:

    simple and helpful, thanks

  53. Thanks says:

    It helped me alot, Thanks.. 🙂

  1. April 18, 2014
  2. September 27, 2014

    […] From: The Big Blob […]

  3. December 4, 2016

    […] C++ FPE. So to get started I need to install it on Raspbian and after searching around I found this guide for Ubuntu which seems to be pretty generic and worked for me; eell up to the part where you copy […]

  4. February 19, 2018
  5. July 18, 2018

    […] Getting started with Google Test (GTest) on Ubuntu […]

  6. January 7, 2019


Leave a Reply

Your email address will not be published.