Reusing object files in Visual Studio 2005

依然范特西╮ 提交于 2019-12-06 08:48:07
Ferruccio

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:

extern "C" MyClass *CreateMyClass();

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.

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

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'.

I think you also need to explicitly add the .obj file to your list of additional dependencies in your project linker settings.

Ha11owed

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

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