views:

247

answers:

3

Been a while since I have programmed in C++, so the whole export/import idea slipped off my mind.

Can you explain me why to use __declspec(dllexport) & import thingy if it looks like I can use classes from other libraries without those.

I have created a solution in VC++ 2005, added the console applicaiton project and two dll libraries projects. Then create ClassA in a LibA, ClassB in LibB project.

Once I have included ClassA.h & ClassB.h into my console app source code, and has linked it with a LibA.lib and LibB.lib I was able to create and use instances of ClassA and ClassB in a console applicaiton. So basically I was able to use classes without exporting/importing them using __declspec.

Can you explain me - what I am missing here.

A: 

You would use __declspec(dllexport) if you wanted to provide symbols in your dll for other dlls/exes to access.

You would use __declspec(dllimport) if you wanted to access symbols in your dll/exe provided by another dll.

Not necessary if you are linking against a static .lib.

sean e
So are you saying I would only need those if I would be linking with a dynamic .lib and loading a dll in a runtime?
Kevin
Yes. See http://msdn.microsoft.com/en-us/library/a90k134d%28VS.80%29.aspx
dirkgently
A: 

If you are including .h files and linking to .lib files then you can drop the DLL declarations. Why do you need a dynamic link library if you only need static linking?

The export declaration marks the function as available for exporting. The declaration you are using may be a macro for "extern" and "pascal" It's been years since I've done this but I think DLLs function calls have a different ordering of pushing params on the stack and the allocation of the return result is done differently (the pascal flag). The extern declaration helps the linker make the function available when you link the library.

You may have missed the step of linking the DLL - the linker will take classA.lib and turn it into classA.dll ( you may need to setup a setupA.def file to define the DLL library). Same applies to ClassB

james
+2  A: 

Once I have included ClassA.h & ClassB.h into my console app source code, and has linked it with a LibA.lib and LibB.lib I was able to create and use instances of ClassA and ClassB in a console applicaiton.

This sounds like you have used static linking. This works without the __declspec(dllexport) in the same way as linking with the object files of your classes directly.

If you want to use dynamic (run-time) linking with a DLL, you have to use either the mentioned declaration or a DEF-file specifying the exported functions. DLLs contain an exports table listing the functions exposed to other executables. All other functions remain internal to your DLL.

Perhaps you are confused coming from the Linux world, where the situation is the other way round: All symbols are visible externally by default.

mkluwe