8

I had a question about DLL building / linking in Visual Studio 2005 and later. Basically my understanding and experience is this:

To build a DLL, I specify the project properties to build a DLL, and then I but __declspec(dllexport) in front of any functions or members that I want to publically expose from the DLL. Building the project will result in a DLL, a Lib, and a header file that can be deployed as say an API or something.

On the other end, to have your other compiled executable application dynamically link to the DLL and use its functions, you simply need to have your executable project include the header files and link with the small lib file that was created when the DLL was built. As long and the compiled application can find the DLL, everything will work.

That has been my experience and that is also how the Microsoft DLL building tutorial described everything on MSDN. I am wondering: is this standard practice? When would you ever need to use __declspec(dllimport) anywhere? Am I missing something?

Thanks!

Russel
  • 2,335
  • 3
  • 19
  • 11
  • possible duplicate of [Why/when is __declspec( dllimport ) not needed?](http://stackoverflow.com/questions/4489441/why-when-is-declspec-dllimport-not-needed) – Hans Passant Jan 18 '11 at 07:38

1 Answers1

14

Yes you would use __declspec(dllimport) and you generally have a macro that controls whether a source file either exports (if it's part of your DLL) or imports (if it's part of the using-executable) symbols.

In your DLL you can set a manifest constant to the build settings of some sort, say 'BUILDING_MY_DLL' and then create the macro like this within your header file:

#ifdef BUILDING_MY_DLL
#define MY_DLL_EXPORT __declspec(dllexport)
#else
#define MY_DLL_EXPORT __declspec(dllimport)
#endif

and then decorate your exported functions like this:

MY_DLL_EXPORT int func(int y);

You can also export entire classes this way too:

class MY_DLL_EXPORT InterestingClass
{
...
};
trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • 2
    Great answer, thanks! I took a look at the source for some DLLs and they appear to all be designed in the way you specified. I was still wondering though, why does this MSDN Microsoft tutorial code get away without using __declspec(dllimport)? -> http://msdn.microsoft.com/en-us/library/ms235636.aspx – Russel Jan 18 '11 at 19:39
  • 4
    @Russel: dllimport isn't required, but I think it's best practise. See this: http://stackoverflow.com/questions/4489441/why-when-is-declspec-dllimport-not-needed – trojanfoe Jan 18 '11 at 20:00
  • 1
    See [Dynamic-Link Libraries](https://msdn.microsoft.com/en-us/library/windows/desktop/ms682589(v=vs.85).aspx) and [Walkthrough: Creating and Using a Dynamic Link Library (C++)](https://msdn.microsoft.com/en-us/library/ms235636(v=vs.140).aspx). All of this is explained more extensively in those. My [Static versus Dynamic Linking](http://simplesamples.info/Beginners/StaticVersusDynamic.aspx) might help a bit. – Sam Hobbs Feb 19 '17 at 22:09