views:

2887

answers:

5

I'm just looking for a simple, concise explanation of the difference between these two. MSDN doesn't go into a hell of a lot of detail here.

+4  A: 

__declspec(dllexport) tells the linker that you want this object to be made available for other dll's to import. It is used when creating a dll that others can link to.

__declspec(dllimport) imports the implementation from a dll so your application can make use it

I'm only a novice c/c++ developer so perhaps someone's got a better explanation than I

rpetrich
+1  A: 

Two different use cases:

1) You are defining a class implementation within a dll. You want another program to use the class. Here you use dllexport as you are creating a class that you wish the dll to expose.

2) You are using a function provided by a dll. You include a header supplied with the dll. Here the header uses dllimport to bring in the implementation to be used by the current program.

Often the same header file is used in both cases and a macro defined. The build configuration defines the macro to be import or export depending which it needs.

morechilli
+2  A: 

Dllexport is used to mark a function as exported. You implement the function in your DLL and export it so it becomes available to anyone using your DLL.

Dllimport is the opposite: it marks a function as being imported from a DLL. In this case you only declare the function's signature and link your code with the library.

Antoine Aubry
+9  A: 

__declspec( dllexport ) - The class or function so tagged will be exported from the DLL it is built in. If you're building a DLL and you want an API, you'll need to use this or a separate .DEF file that defines the exports (MSDN). This is handy because it keeps the definition in one place, but the .DEF file provides more options.

__declspec( dllimport ) - The class or function so tagged will be imported from a DLL. This is not actually required - you need an import library anyway to make the linker happy. But when properly marked with dllimport, the compiler and linker have enough information to optimize the call; without it, you get normal static linking to a stub function in the import library, which adds unnecessary indirection. ONT1 ONT2

Shog9
A: 

Suppose u r defining a class within a dll and another class wanted to use the same function .At that time we will use dllexport to that function so that the other function cak also use this function

swati