Search code examples
c++linker-errorsgoogletest

Linker error - linking two "application" type projects in order to use Google Test


I am trying to test a function with Google Test.

It seems that everything is set up correctly, and it builds and executes fine without gtest... (There is a bit of complexity in the code, so I cannot list all the source files here, but without adding gtest, the files are linking properly, and running as they should).

It is an application type project. It has a number of library dependencies... irrelevant.

The test project is added as a separate project to the solution. It has the tested project as a dependency. The .h file of the test project only points to the gtest... The .cpp (not main, which is the standard InitGoogleTest main) adds its own header file, the header file of the tested project, and has the test shown below.

There is a TestedProject.lib created automatically, when the project builds, even though it is an application. I have added TestedProject.lib as a library dependency to the TestProject (in Link).

Class x
{
public:
  x(){}    // I didn't really need this, I only added the class so I have access to 
  ~x(){};  // non-class methods with gtest - but it still doesn't work
  bool myFunction(std::string a, double b, bool c);  
};

implementation:

bool x::myFunction(std::string a, double b, bool c)
{
  // implementation
  return false;
}

somewhere_else
{
  x x_instance;
  y = x_instance.myFunction("a", 1, false);   // works, all builds, executes, life is great
}

Add unit test:

class TheTest : public ::testing::Test
{
protected:
    x x_instance;
};

TEST_F(TheTest, Fail)
{
    EXPECT_FALSE(x_instance.myFunction("a", 1, false));     
}

Doesn't build. Link error (modified, like the sample code above, with simplified names, I hope I didn't mess up content)

Error   2   error LNK2019: unresolved external symbol 
"public: bool __thiscall x::myFunction(class std::basic_string<char,struct std::char_traits<char>,double,bool)" 
(?myFunction@x@@QAE_NV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@00000NNNN_N1@Z) 
referenced in function "private: virtual void __thiscall TheTest_Fail_Test::TestBody(void)" 
(?TestBody@TheTest_Fail_Test@@EAEXXZ)   C:\path\file.obj

I have done this before - solved link errors - wrote a couple of tests with Google test - but I can't see anything missing.

As a test, I wrote a little

int test(){return 4;} in the header file, in the class declaration...

and then, replaced the test with EXPECT_EQ(x.test(), 4);

It worked. Great. But that would mean to have all the tested code in a single file, a cpp or something... which is simply unreasonable. There are a few files in this Application project.

How can I fix this issue ? How can I make Google Test link and test with a class with a header and an implementation file ? When that header/implementation is in a different project, of "Application" type ?

The only similar issue I have found, so far: C++ linking issue on Visual Studio 2008 when crosslinking different projects on same solution

Please, help me find a solution.


Solution

  • So I'll have an answer:

    I have 2 solutions to my question:

    1) Break up the application project in 2 projects, one will become a library, with most of the code; the other will be an application, containing a tiny main() that calls the entry point of the real code (like a parameter parsing method or something).

    Then, I can add a unit testing project - to test the lib.

    2) Don't break up the project. Add a gtest project, don't create any dependencies. Add the files to test into the gtest project. The gtest project will be a separate executable... with all it needs to be happy. (Advantage: no dependencies for testing)

    I prefer the first version.