My solution has three projects: GoogleTest (for using Google Test), Vi (for the bulk of the logic) and ViTests (for the unit tests using Vi). The ViTests project references the Vi project and the Google Test project.
Vi has the following code in v1.h
#pragma once
namespace Vi
{
class Vi1
{
public:
int SomeInt();
};
}
And the matching v1.cpp
#include "vi1.h"
namespace Vi
{
int Vi1::SomeInt()
{
return 123;
}
}
The test function in ViTests follows
TEST(Vi1Foo, SomeIntIsSame)
{
Vi1 v = Vi1{};
EXPECT_EQ(123, v.SomeInt());
}
The linker error says there's an unresolved symbol SomeInt
. However, I can make the linker error go away by inlining the function like so:
namespace Vi
{
class Vi1
{
public:
int SomeInt() { return 123; }
};
}
Why is the unit test project not finding the SomeInt
function definition when it's placed in a separate cpp file?
Thanks.
Extra details incase useful: I'm using Visual Studio 2015.
The error message:
Error LNK2019 unresolved external symbol "public: int __thiscall Vi::Vi1::SomeInt(void)" (?SomeInt@Vi1@Vi@@QAEHXZ) referenced in function "private: virtual void __thiscall ViTests::Vi1Foo_SomeIntIsSame_Test::TestBody(void)" (?TestBody@Vi1Foo_SomeIntIsSame_Test@ViTests@@EAEXXZ) Vi_Tests C:\Users\MyName\Vi\Vi_Tests\Vi_Tests.obj 1
Project types: Vi is Win32 Application, ViTests is Win32 Console Application, GoogleTest is a static library.