How to setup Google Mock with Eclipse CDT and CMake. Google Mock also includes Google Test.

Create a new C++ project in Eclipse CDT

  1. Start Eclipse CDT
  2. Select workspace folder
  3. Close the Welcome screen
  4. Select “File - New - Other…”
  5. Select “C/C++ - C++ Project”
  6. Enter Project Name:
  7. Select Project Type ‘‘Makefile project - Empty Project’’ and toolchain ‘‘Linux GCC’’
  8. Click “Finish”
  9. Select Yes if asked to open the C/C++ perspective

Build Google Test and Google Mock in Eclipse using CMake

Now we will import Google test and Google mock into our Eclipse project and build them with a minimalistic main file.

  1. Create a folder with name google on your harddrive
  2. Download Google Mock zip file ( to your new google folder and unpack
  3. Right click on the TestProj project in Project Explorer and select New - Folder
  4. Enter folder name google and click Finish
  5. Right click on the google folder and select Import…
  6. Select General - File system and click Next
  7. Click Browse…
  8. Select the google folder that you previously downloaded Google Mock to
  9. Check the google folder and click Finish
  10. You should now have a gmock-1.7.0 folder inside the google folder in the Project Explorer
  11. Create a new folder test by right clicking on TestProj and selecting New - Folder
  12. Right click on the test folder and select New - Source File
  13. Enter name main.cpp and click on Finish
  14. Add the following code to main.cpp and save:
    #include "gmock/gmock.h"
    int main(int argc, char* argv[]) {
      ::testing::InitGoogleMock(&argc, argv);
      return RUN_ALL_TESTS();

  15. Right click on the TestProj project and select New - File
  16. Select TestProj root folder as Parent Directory, enter name “CMakeLists.txt” and click Finish
  17. Add the following to the CMakeLists.txt and save:
    #Set minimum CMake version required to run this file
    cmake_minimum_required(VERSION 2.8)
    #Set name of project 
    #Set compiler flags:
    #  std=c+11 <-- Add support for C++11 features
    #  g3 <-- Include debugging information in build
    #  Wall <-- Enable all compiler warnings messages
    add_definitions(-Wall -g3 -std=c++11)
    #Add the given directories to those the compiler uses to search for include files
    #  CMAKE_CURRENT_SOURCE_DIR <-- This is the directory where the currently processed CMakeLists.txt is located in 
    #Add a subdirectory to the build. The directory specified must contain a CMakeLists.txt file.

  18. Create another CMakeLists.txt file inside the test folder
  19. Add the following to the CMakeLists.txt file inside the test folder
    #Set a cache and an environment variable
    set(GMOCK_DIR "../google/gmock-1.7.0"
        CACHE PATH "The path to the GoogleMock test framework.")
    #Add a subdirectory to the build. The directory specified must contain a CMakeLists.txt file.
    #  GMOCK_DIR value has been set with the set command
    #  CMAKE_BINARY_DIR <-- The path to the top level of the build tree. That is the directory from where cmake is run.
    add_subdirectory(${GMOCK_DIR} {CMAKE_BINARY_DIR}/gmock)
    #Add the given directories to those the compiler uses to search for include files
    include_directories(SYSTEM ${GMOCK_DIR}/gtest/include
    #The add_executable command tells CMake to create a binary
    #The first argument is the name of the binary to create, the
    #rest are source files. Header files are not included in this
    add_executable(testprojtest main.cpp)
    #target_link_libraries specifies libraries or flags to use 
    #when linking a given target. The named target (first 
    #argument) must have been created in the current directory
    #with add_executable() or add_library()
    target_link_libraries(testprojtest gmock_main)

  20. Create a new folder build by right clicking on TestProj and selecting New - Folder
  21. Now we will create a Make Target to generate the Makefiles from the CMakeLists.txt files.
  22. Select the Make Target tab on the right side of Eclipse
  23. Expand the TestProj project and right click on the build folder
  24. Click on New…
  25. Enter Target name: cmake
  26. Uncheck Same as the target name and make the Make target box empty
  27. Uncheck Use builder settings and enter Build command: cmake .. (please note the space followed by two dots at the end)
  28. Click on OK
  29. Double-click on the new Make Target cmake. Some files and folders shall be generated in the build folder. You can verify this in the Project Explorer on the left side of Eclipse.
  30. Now we will configure some TestProj project settings so that we can build
  31. Right click on the TestProj project in the Project Explorer and select Properties
  32. Go to “C/C++ Build”, select tab “Builder Settings” and click on Workspace… button
  33. Expand the folder tree and select the build folder. Click OK
  34. Click on Apply
  35. Click on OK to exit the project properties for TestProj
  36. Select menu Project - Build Project (click on TestProj in the Project Explorer first if this menu item is gray)
  37. Open the Console tab and check that the build went fine
  38. Select menu Run - Run Configurations…
  39. Select C/C++ Application on the left and click on the New button
  40. Enter Name: TestProjConsole
  41. Click on button Search Project… and select testprojtest. Click OK.
  42. You should now have build/test/testprojtest in the C/C++ Application field
  43. Click on Apply and then on Run
  44. You should get the following in the Console tab:
    [==========] Running 0 tests from 0 test cases.
    [==========] 0 tests from 0 test cases ran. (0 ms total)
    [  PASSED  ] 0 tests.
  45. This means that we have run all our Google Tests. However currently we have 0 tests in our project.

Create a test

We will create a test for a class called AddClass that can be used to add numbers.

  1. Create a new file in the test folder with name: AddClassTest.cpp
  2. Add the following to the new file:
    #include "gmock/gmock.h"
    #include "AddClass.h"
    TEST(AddClassTest, CanAddTwoNumbers)
      AddClass myAddClass;
      int sum = myAddClass.add(1,2);
      ASSERT_EQ(3, sum);
  3. Locate the following line test/CMakeLists.txt:
    add_executable(testprojtest main.cpp)

    Add the new file AddClassTest.cpp to the line:

    add_executable(testprojtest main.cpp AddClassTest.cpp)
  4. Build project via Project - Build Project
  5. The build should fail because of the missing AddClass implementation
  6. Create a new folder in the TestProj project with name: src
  7. Create a new file in the src folder with name AddClass.h and the following content:
    #ifndef ADDCLASS_H_
    #define ADDCLASS_H_
    class AddClass
      int add(int arg1, int arg2);
  8. Create a new file in the src folder with name AddClass.cpp and the following content:
    #include "AddClass.h"
    int AddClass::add(int arg1, int arg2)
      return arg1 + arg2;
  9. Now we include the src folder when building
  10. Locate the following line in the top CMakeLists.txt file:

    Add the following line below it:

  11. Create a CMakeLists.txt file in the src folder with the following content:
    add_library(src AddClass.cpp)
  12. Add the following line to the top CMakeLists.txt file:
  13. Finally we must make the new src library known to the test application by adding the following line at the end of test/CMakeLists.txt:
    target_link_libraries(testprojtest src)
  14. Build and run project
  15. You should now get the following output in the Console tab:
    [==========] Running 1 test from 1 test case.
    [----------] Global test environment set-up.
    [----------] 1 test from AddClassTest
    [ RUN      ] AddClassTest.CanAddTwoNumbers
    [       OK ] AddClassTest.CanAddTwoNumbers (0 ms)
    [----------] 1 test from AddClassTest (1 ms total)
    [----------] Global test environment tear-down
    [==========] 1 test from 1 test case ran. (5 ms total)
    [  PASSED  ] 1 test.