Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the code "DECLDIR __declspec(dllexport)" really do?

Tags:

c

file

dll

#ifndef _DLL_TUTORIAL_H_
#define _DLL_TUTORIAL_H_
#include <iostream>

#if defined DLL_EXPORT
#define DECLDIR __declspec(dllexport)
#else
#define DECLDIR __declspec(dllimport)
#endif

extern "C"
{
   DECLDIR int Add( int a, int b );
   DECLDIR void Function( void );
}

#endif

What does the code DECLDIR __declspec(dllexport) really do?

like image 619
deepak Avatar asked Feb 20 '10 07:02

deepak


2 Answers

In the Microsoft world, __declspec(dllexport) makes a function or class callable from outside the DLL.

When you create a DLL, by default, any functions defined within the DLL are only callable from that same DLL. You cannot call that function from an executable or a different DLL.

If you want your a function to be called from outside the DLL, you need to export it by adding __declspec(dllexport).

One way to think about it is that __declspec(dllexport) marks a function as being part of a DLL's public interface.

While you didn't ask about __declspec(dllimport) that is sort of the opposite. When calling a function in a different DLL, your DLL needs to know that it's part of a different DLL's public interface so it can properly handle the call (calling a function in a different DLL requires more complex code that calling a function in yourself).

like image 76
R Samuel Klatchko Avatar answered Oct 12 '22 04:10

R Samuel Klatchko


It defines the DECLDIR macro constant to be __declspec(dllexport). dllexport is for exporting functions from DLLs. Here's a quote from this page:

These attributes explicitly define the DLL's interface to its client, which can be the executable file or another DLL. Declaring functions as dllexport eliminates the need for a module-definition (.DEF) file, at least with respect to the specification of exported functions. Note that dllexport replaces the __export keyword.

If a class is marked declspec(dllexport), any specializations of class templates in the class hierarchy are implicitly marked as declspec(dllexport). This means templates are explicitly instantiated and its members must be defined.

__declspec, by the way, is explained here.

like image 29
Eli Bendersky Avatar answered Oct 12 '22 04:10

Eli Bendersky