开发者

Configure a Visual C++ 2010 project to use a DLL

I've learned how to make a DLL, it was easy to make its unit tests, because it appeared in the project configuration that my DLL project s开发者_开发问答ince it was in the same solution file.

Now I'm starting another project but I want to use either my DLL with it or another DLL, where do I tell visual to use this or this DLL ?

I configured visual to use the correct headers, but how can I make it find the DLL file ?


You can make this automatic in Visual Studio so that the user of your DLL can't forget this. The typical DLL header file could look like this:

#undef MYEXPORTS
#ifdef BUILDING_MYDLL
#  define MYEXPORTS __declspec(dllexport)
#else
#  define MYEXPORTS __declspec(dllimport)
#  pragma comment(lib, "mydll.lib")
#endif

MYEXPORTS void SomeFunction();
// etc..

The #pragma directive injects a linker option into the .obj file that ensures the linker always looks for the .lib when a client program #includes the header file. This is the same mechanism by which it always looks for the correct version of the CRT .lib file even though you never explicitly mention it on the Additional Dependencies setting. This is otherwise non-standard, but using DLLs is non-standard anyway.


You need to add the .LIB file to your project. It should have been generated when you compiled the DLL and will be in the same directory as the compiled DLL.

In VS2008 you would open up the Project Properties and go to Linker, Input, Additional Dependencies and specify the LIB file there. It should be pretty similar in VS2010.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜