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

北城以北 提交于 2019-12-08 02:31:47

问题


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.


回答1:


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.




回答2:


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.




回答3:


There is another solution for this.

Just create a new project for Google Test Framework. (of course, under your existing application solution).

And then, after you make sure all your Google Test Framework setup correctly. (you can test it under the newly created solution)

Manually include the code your want test (use Add -> Existing Item) from your main project, and then you can test your code without generate additional lib.

The good part of this is that when you test some application which requires DLL from windows, it requires the application uses Multi-threaded Debug DLL. (in your Project property settings, go to C/C++ -> Code Generation -> Runtime Library see what you got)

And Google Test Framework uses a very different RunTime Library (Multi-threaded Debug (/MTd)).

At the stage of linking, the compiler will cry that it has some difficulty links the generated lib from your application with Multi-Thread DLL and the google framework's lib (which is Multi-threaded).

In this way, you can avoid the dependency problem of both project. (one for /Mtd and one for /Md)



来源:https://stackoverflow.com/questions/15283699/linker-error-linking-two-application-type-projects-in-order-to-use-google-te

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!