在Visual Studio 2005中重用对象文件 [英] Reusing object files in Visual Studio 2005

查看:249
本文介绍了在Visual Studio 2005中重用对象文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里的情况:

我有一个VS2005解决方案,有两个项目:MyDll(DLL),MyDllUnitTest(console EXE)。

I have one VS2005 solution with two projects: MyDll (DLL), MyDllUnitTest (console EXE).

在MyDll中,我有一个名为MyClass的类,它在DLL的内部,不应该导出。我想在MyDllUnitTest中测试它,所以我添加了一个名为MyClassTest的测试套件类,其中我创建了MyClass的实例并测试它们。

In MyDll I have a class called MyClass which is internal to the DLL and should not be exported. I want to test it in MyDllUnitTest, so I added a test suite class called MyClassTest, where I create instances of MyClass and test them.

我的问题:通过构建MyDll创建的MyClass的目标文件到MyDllUnitTest EXE?我不想在MyDllUnitTest中构建MyClass,我不想导出类。

My question: how can I link the object file of MyClass, created by building MyDll, to the MyDllUnitTest EXE? I don't want to build MyClass in MyDllUnitTest and I don't want to export the class.

我尝试使用相同的中间目录对于两个项目(所以对象文件在同一目录),并使用VS2005的引用功能(右键单击项目 - >参考 - >添加新参考...),但它没有工作 - 我还是得到一个链接错误(LNK2001)。

I tried using the same Intermediate Directory for both projects (so object files are in the same directory) and using the References feature of VS2005 (right click project --> References --> Add New Reference...), but it didn't work - I still get a linking error (LNK2001).

编辑:我不想在两个项目中使用相同的源文件 - 有很多MyClass / MyClassTest,这意味着我必须将每个MyClass复制到不同的项目。
我知道可以在两个项目中使用相同的目标文件,我已经看过它之前,但忘了怎么做。

I don't want to have the same source file in two projects - consider the face that I have many MyClass/MyClassTest, which means I have to duplicate each MyClass to a different project. I know it is possible to use the same object file in two projects, I've seen it done before but forgot how.

编辑: 我决定把这两个项目的文件,所以他们编译两次。原因是参考功能自动工作 - 但仅适用于静态库项目。

I've decided to put the files in both projects, so they are compiled twice. It turns out the "Reference" feature works automatically - but only for static lib projects.

推荐答案

不想在你的dll项目中构建它。只要两个项目都使用相同的源文件,它们都将生成相同的目标文件(假设编译器选项设置方式相同)。

I don't understand why you don't want to build it in your dll project. As long as both projects are using the same source file, they will both generate the same object file (assuming compiler options are set the same way).

如果你想测试dll而不导出类本身(我认为这是因为导出类中的类通常是一个坏主意),考虑从dll导出工厂函数。它将有一个签名如:

If you want to test the dll without exporting the class itself (I presume this is because exporting classes in a dll is usually a bad idea), consider exporting a "factory" function from the dll. It would have a signature like:

extern "C" MyClass *CreateMyClass();

此函数将创建一个MyClass对象并返回一个指针。您的单元测试随后可以使用返回的类对象完成任何所需。

This function would create an object of MyClass and return a pointer to it. Your unit test can then do whatever it needs with the returned class object.

这篇关于在Visual Studio 2005中重用对象文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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