在其他项目Visual Studio中使用控制台应用程序类中的c ++类 [英] Use c++ classes from console application class in other project visual studio

查看:79
本文介绍了在其他项目Visual Studio中使用控制台应用程序类中的c ++类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在相同的解决方案中,我试图在Visual Studio中为我的控制台应用程序创建一个gtest项目.当我从新项目"创建此项目时,向导,我可以选择我要测试的项目.因此,我选择了我的控制台应用程序.在Visual Studio创建了gtest项目之后,我可以在引用下看到存在我的控制台项目.当我尝试示例测试用例时,一切正常.但是,当我开始从控制台应用程序中包含我的班级时,出现链接器错误,该错误表明班级上的符号未解析(LNK_2019).我知道这通常意味着找到了方法的声明(.h文件),但是没有找到定义(编译的.cpp文件).通常,如果出现此错误,我会将.lib文件添加到链接器设置中,但似乎控制台应用程序不会输出lib文件.有什么方法可以在测试项目的控制台应用程序中使用我的类?

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?

推荐答案

您应设计用于测试的应用程序.

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.

现在,您可以创建与库链接的测试.请注意,这是一种通用模式,不仅限于gtest框架.

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.

这篇关于在其他项目Visual Studio中使用控制台应用程序类中的c ++类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆