0

So i need to use gTest.

I have a util class with static functions.

The header file contain the declerations of the functions:

class a
{
public:
    a();
    virtual ~a();

    static bool test();
}

on the cpp file the implementations:

a::a() { }
a::~a() { }
bool a::test() { return true; }

on the test unit file i just added a test :

TEST(a, a)
{
  EXPECT_EQ(true,a::test());  
}

I'm getting a linker error :

Error   3   error LNK2001: unresolved external symbol "public: static bool __cdecl a::test()" (?test@a@@SA_NIPAD0@Z)    UnitTest.obj    UnitTest

if the implementation of the static function is in the .h file , everything work smoothly.

is there anyway to uni test static function that way?

USer22999299
  • 5,284
  • 9
  • 46
  • 78
  • 2
    Did you simply miss to link your `.cpp` file to the testrunner? – πάντα ῥεῖ Apr 30 '14 at 08:11
  • How should i do it? usually i'm linking the dll lib. – USer22999299 Apr 30 '14 at 08:15
  • @USer22999299 What dll lib are you referring to? – Mike Kinghan Apr 30 '14 at 08:20
  • I have the Unitest project which i references to GoogleTest and to my project that i want to uni test. In addition to that in the additional dependencies in the linker property , i added the lib of my project that im trying to test. – USer22999299 Apr 30 '14 at 10:43
  • 2
    You mentioned that you're linking a DLL library. If that's the case, then you need to [export the function](http://msdn.microsoft.com/en-us/library/a90k134d.aspx) in order for the test to invoke it. If you don't do this, the linker won't be able to find the implementation. – Lilshieste Apr 30 '14 at 12:47
  • 1
    If this is your first exposure to C++ exports, [the answer in this question](http://stackoverflow.com/a/538179/926713) might be a useful reference, as well. – Lilshieste Apr 30 '14 at 15:59

1 Answers1

1

Currently, you implement test() without a return type. Which sometimes might be interpreted as default int. Add the type by changing this:

a::test() { return true; }

into this:

bool a::test() { return true; }

However it seems weird that the compiler did not complain earlier. It is still possible that the compiled file is not linked correctly.

Try changing the code first.

meandbug
  • 202
  • 2
  • 5