在 Visual Studio 2005 中重用对象文件
情况是这样的:
我有一个 VS2005 解决方案,有两个项目:MyDll (DLL)、MyDllUnitTest (控制台 EXE)。
在 MyDll 中,我有一个名为 MyClass 的类,该类位于 DLL 内部,不应导出。 我想在 MyDllUnitTest 中测试它,因此我添加了一个名为 MyClassTest 的测试套件类,在其中创建 MyClass 的实例并测试它们。
我的问题:如何将通过构建 MyDll 创建的 MyClass 的目标文件链接到 MyDllUnitTest EXE? 我不想在 MyDllUnitTest 中构建 MyClass 并且不想导出该类。
我尝试对两个项目使用相同的中间目录(因此目标文件位于同一目录中)并使用 VS2005 的引用功能(右键单击项目 --> 引用 --> 添加新引用...),但它不起作用 - 我仍然收到链接错误(LNK2001)。
编辑:我不想在两个项目中拥有相同的源文件 - 考虑一下我有很多 MyClass/MyClassTest,这意味着我必须将每个 MyClass 复制到不同的项目。 我知道可以在两个项目中使用相同的目标文件,我以前见过它,但忘记了如何做。
编辑:我决定将文件放入两个项目中,因此它们会被编译两次。 事实证明,“参考”功能会自动工作 - 但仅适用于静态库项目。
Here's the situation:
I have one VS2005 solution with two projects: MyDll (DLL), MyDllUnitTest (console EXE).
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.
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.
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).
Edit: 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.
Edit: 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我不明白为什么你不想在你的 dll 项目中构建它。 只要两个项目使用相同的源文件,它们都会生成相同的目标文件(假设编译器选项设置相同)。
如果您想在不导出类本身的情况下测试 dll(我认为这是因为在 dll 中导出类通常是一个坏主意),请考虑从 dll 导出“工厂”函数。 它的签名如下:
该函数将创建 MyClass 的对象并返回指向它的指针。 然后,您的单元测试可以对返回的类对象执行所需的任何操作。
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).
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:
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.
这是实现您尝试执行的操作的另一种方法,但我相信它将满足您的要求...
在包含要测试的类的程序集中使用 InternalsVisibleToAttribute 属性。 然后,如果您引用此程序集,您将能够测试该类,即使对于其他程序集这些类型是“不可见的”。 魔法!
以下是要使用的属性的 MSDN 参考...
http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute.aspx
Here is an alternative approach to achieve what your trying to do BUT I believe that it will meet your requirements...
Use the InternalsVisibleToAttribute attribute on the assembly that contains the classes you want to test. Then if you reference this assembly you'll be able to test the class even though to other assemblies these types are "invisible". Magic!
Here is the MSDN reference of the attribute to use ...
http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute.aspx
我在这里所做的是在测试项目中创建一个额外的“过滤器”“对象文件”,以及“头文件”和“源文件”,并将所有必要的 .obj 文件放入其中(如果它们已经顺便说一下,已经生成了)。 这似乎在这里工作得很好。 我们在这里也使用它进行单元测试,在某些地方,当将它用于两个不同的 DLL 时,不必两次编译相同的源文件。
我们这样做的原因之一是我们使用 CMake 来生成项目文件,因此无法使用所有内部 Visual Studio“魔法”。
What I do here is making an extra 'filter' "Object Files" in the testing project, alongside the "Header Files" and "Source Files", and putting all the necessary .obj files in there (which is easier if they've already been generated, by the way). This seems to work fine here. We also use that for unit testing here, and in some places for not having to compile the same source file twice when using it for two different DLLs.
One reason we're doing it this way is that we use CMake to generate our project files, and so cannot use all of the internal Visual Studio 'magic'.
我认为您还需要将 .obj 文件显式添加到项目链接器设置中的其他依赖项列表中。
I think you also need to explicitly add the .obj file to your list of additional dependencies in your project linker settings.
您还可以尝试使用生成包含所有对象的 .lib 的命令。
就像来自的答案: Visual C++ 链接从引用的项目生成对象< /a>
you could also try to use a command that generates a .lib with all the objects.
Like in the answer from: Visual C++ link generated objs from referenced project