9

I'm attempting to integrate googlemock into my tests. I had already successfully built and run tests on googletest, and now am trying to incrementally add the gmock functionality into the tests as well, but I've hit a compile error that I utterly do not understand.

I am not attempting to use or define mocked classes, or use anything gmock.h provides. At the top of my (previously working) tests.cpp file I merely type

#include "gmock/gmock.h"

And I get the compile error:

gmock/gmock-matchers.h(2497) : error C2059: syntax error : 'sizeof'

gmock/gmock-matchers.h(2505) : see reference to class template instantiation 'testing::internal::ElementsAreMatcherImpl' being compiled

gmock/gmock-matchers.h(2497) : error C2059: syntax error : ')'

gmock/gmock-matchers.h(2497) : error C2143: syntax error : missing ')' before '{'

gmock/gmock-matchers.h(2497) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

gmock/gmock-matchers.h(2499) : warning C4183: 'Message': missing return type; assumed to be a member function returning 'int'

I'm compiling this using nmake/vc++ on Windows 7, and I can't fathom why I would get these compile errors simply from adding the core gmock include file to my test file. Has anyone seen this sort of thing before?

Raildex
  • 3,406
  • 1
  • 18
  • 42
whazzmaster
  • 566
  • 1
  • 4
  • 16
  • I remember something about the order of includes being important? have you tried putting gmock.h before and/or after gtest.h ? – Kyle C May 24 '12 at 16:19
  • Yeah, I've tried varying the #include order but received the same error. – whazzmaster May 24 '12 at 16:32
  • Why you want to include gmock.h at all in a test-file? It should be sufficient to include your Mock to your testfile. Does it work for you, if you create a Fixture.h which is included by the testfile, and to include gmock.h in this fixture? – Alex Sep 07 '12 at 15:13
  • @whazzmaster: I am also getting the same error. How did you resolve it? – NJMR Mar 14 '16 at 04:32

3 Answers3

4
  1. Did you init google mock with InitGoogleMock(&__argc, __argv) in test project's main function?
  2. You should include only "gmock/gmock.h" in your test files (and where you call InitGoogleMock) - no need for inclusion of gtest.h.
  3. Have you updated your googletest library to googlemock. (https://github.com/google/googletest)

If all the things above are true it should work.

Doge Person
  • 121
  • 6
0

Things to make sure before using gtest and gmock.

  1. Include the gmock lib in Android.bp file.
  2. Include the header "external/googletest/googlemock/include" in include_dirs in Andoird.bp.
  3. Initialize the gmock and gtest in your test main function.
    testing::InitGoogleTest(&argc, argv);
    testing::InitGoogleMock(&argc, argv);
-1
#include <iostream>
#include "gmock/gmock.h"

template <typename T>
class Foo : private T {
public:
  void foo() {
    T::bar();
  }

};

class Bar {
public:
  void bar() {
    std::cout << "Hey there!" << std::endl;
  }
};



int main() {
  Foo<Bar> f;
  f.foo();
}

template <typename T>
class Foo : private T {
public:
  void foo() {
    T::bar();
  }
};

class Bar {
public:
  void bar() {
    std::cout << "Hey there!" << std::endl;
  }
};



int main() {
  Foo<Bar> f;
  f.foo();
}
Raildex
  • 3,406
  • 1
  • 18
  • 42
  • This code-only answer doesn't attempt to answer the question at all? The issue comes from _including `gmock.h`_ -- and all this answer is doing is showing an example of... some really unrelated code with duplicate symbols? I'm recommending for deletion; I have no idea what this even is. – Human-Compiler Aug 05 '22 at 21:23
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 06 '22 at 16:32