How to include Google Mock in CMake downloading from Github
Asked Answered
B

4

5

In the quickstart of google test (https://google.github.io/googletest/quickstart-cmake.html) I found the following code to download the google test dependencies from Github:

cmake_minimum_required(VERSION 3.14)
project(my_project)

# GoogleTest requires at least C++14
set(CMAKE_CXX_STANDARD 14)

include(FetchContent)
FetchContent_Declare(
  googletest
  URL https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip
)

FetchContent_MakeAvailable(googletest)

enable_testing()

add_executable(
  hello_test
  hello_test.cc
)
target_link_libraries(
  hello_test
  gtest_main
)

include(GoogleTest)
gtest_discover_tests(hello_test)

This works for google test, and in the test file hello_test.cc I can include #include "gtest/gtest.h" successfully.

However, I would like to include also Gmock: #include "gmock/gmock.h" but it cannot find it.

How can I include gmock downloading the dependencies like gtest?

Bop answered 5/7, 2022 at 11:33 Comment(2)
Possibly unrelated: are you sure you have #include "gtest.gtest.h"? This filename is not typical for the Google Test header. The #include "gtest/gtest.h" and #include "gmock/gmock.h" are the usual ones.Coze
Thank you for pointing it out, I mispelled. Edited now.Bop
B
9

So after some try and error, found out that linking the gmock library is enough, like so:

target_link_libraries(hello_test gtest_main gmock_main)

I'm not sure why this works though, I would have expected cmake to include the headers regardless of the linking phase.

I have probably some basic misunderstanding of how cmake works so I would appreciate any clarification.

Bop answered 6/7, 2022 at 6:50 Comment(2)
Please, do not post a comment as the answer, you can edit your question and add some new information to clarify your needs.Cotquean
@Cotquean would downvote your comment if I could. This answer is actually the solution and 100% an answer. If posted as comment I would ask OP to please post it as an answer instead because solutions and even possible solutions *are answers*.Backsight
P
3

This makefile will attempt to download the google test source to your local build directory, underneath a folder called _deps.

First check whether you actually have that folder, and then verify whether it contains gmock.h (it'll be inside a directory called googletest-src). If it isn't present, then something possibly went wrong with your download.

Also, please post the platform that you are working on, and the exact error that you are getting when you try to build.

Piliferous answered 5/7, 2022 at 20:25 Comment(2)
I'm working on macOS, and the build error is: 'gmock/gmock.h' file not found. In the googletest-src folder there is googlemock/include/gmock/gmock.h, so I'm just missing the step to let cmake find it.Bop
That does not answer the question. Anyone with a find utility can tell you that. To restate the question: why do the search paths not show up in CMake for gmock, and yet they show up for GTest ?Ridotto
C
0

As @mortimer mentioned by doing the FetchContent both googlemock and googletest will be dowloaded in your build folder under _deps/googletest-src.

You are making available googletest and you should also make available googlemock like so:

FetchContent_MakeAvailable(googletest)
FetchContent_MakeAvailable(googlemock)

That is why the only thing you need to do is to link the libraries you need for your tests as you did with:

target_link_libraries(hello_test gtest_main gmock_main)
Conglutinate answered 6/1, 2023 at 2:16 Comment(0)
V
0

Heyo,

I believe its a combination of fixes. I had spent a day trying to fix it.

  1. You need gmock_main

target_link_libraries(hello_test gtest_main gmock_main)

  1. Fix your fetch command. You need the following:

set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)

Add it like so:

include(FetchContent)
FetchContent_Declare(
  googletest
  URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
Vogler answered 3/7, 2023 at 19:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.