Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 307
Thalia Avatar asked Mar 07 '13 23:03

Thalia


People also ask

What causes linker errors?

Linker errors occur when the linker is trying to put all the pieces of a program together to create an executable, and one or more pieces are missing. Typically, this can happen when an object file or libraries can't be found by the linker. Fixing linker errors can be tricky.

What is Test_f in Gtest?

TEST_F(TestFixtureName, TestName) { ... statements ... } Defines an individual test named TestName that uses the test fixture class TestFixtureName . The test suite name is TestFixtureName .


1 Answers

There is a another solution, which I prefer because it means you can avoid to change your main project:

Add a "post build action" to the main project in order to create a static library for the exact same source files. Then you can simply add this dependency to you gtest project.

Each time you'll compile your main project it will build the application AND the static library.

This way you don't have to create a third project and keep configurations synchronized.

Hope it helps.

like image 55
toussa Avatar answered Sep 20 '22 16:09

toussa