Use c++ classes from console application class in other project visual studio

浪尽此生 提交于 2021-01-29 08:00:16

问题


I am trying to make a gtest project in Visual Studio for my console application in the same solution. When i create this project from the "new project" wizard i can choose the project i want to test. So i select my console application. After visual studio has created the gtest project i can see under references that my console project is present. When i try the sample test case everything works. But when i start to include my classes from the console application i get a linker error which says a symbol on my class is unresolved (LNK_2019). I know this generaly means that the declaration (.h file) of the method is found but the definition (compiled .cpp file) is not found. Normally if i get this error i add the .lib file to the linker settings, but it seems that a console application does not output a lib file. Is there any way to use my classes in my console application in my test project?


回答1:


You should design you application for testing.

Split the application into two projects. The core logic should be implemented in a (static) library. The actual command line application is just a small shell around the logic that has been implemented in the library.

Now you can create the test that links with the library. Please note that this is a general pattern and not limited to the gtest framework.



来源:https://stackoverflow.com/questions/64537645/use-c-classes-from-console-application-class-in-other-project-visual-studio

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