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

落花浮王杯 提交于 2019-12-06 13:30:35

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.

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.

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)

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