Re: DLL exporting with __declspec or .DEF file?
On 15 Sep 2006 19:47:58 -0700, sjnorris123@yahoo.com wrote:
I've been reading several posts, MSDN articles and VC Help but it's
still not clear to me whether or not an EXE has to be recompiled if an
implicitly linked DLL changes.
Currently, all of our applications are statically linked and we are
considering converting our library code to a DLL. From what I've read,
implicit linking seems to be the simpliest to implement but I'm
concerned about the statements I've read that says if you are using
__declspec to export/import and you change the DLL then you have to
recompile the EXE's that implicitly link to the export lib. We are
using purely C code, no C++ and the testing that I've done seems to
indicate that additional functions can be exported from the DLL and the
EXE's run fine without recompiling. Is this just by shear luck?
The only time you have to worry about adding functions to C code is when
importing by ordinal; you don't want to change the ordinals your clients
are using. But __declspec(dllexport|dllimport) doesn't use ordinals for
linking; it's based on string (name) matching, so there is no ordering to
mess up.
I guess I'm trying to understand if using __declspec exports by name or
ordinal and if using __declspec is the way we should go or if we should
be using the .DEF method, though this method seems to carry a high
maintenance overhead. All of the code is under our control and the DLL
is meant to be used only with our applications. We simply think
switching our library code over to a DLL will allow us to provide bug
fixes to existing code without having to redistribute all the
applications that use the library functions and add new library
functions needed for new applications.
Any comments, insights or suggestions will be greatly appreciated.
The __declspec method is a lot more convenient that .DEF files, especially
in C++. Unless I have a specific reason to use a .DEF file, I use
__declspec. Below is a message I've posted several times concerning the
right way to use __declspec:
In some DLL header file, which the other DLL headers #include, write the
following, replacing "X" with the name of your DLL, making sure it has a
reasonable chance of being unique:
#ifdef COMPILING_X_DLL
#define X_EXPORT __declspec(dllexport)
#else
#define X_EXPORT __declspec(dllimport)
#endif
Then #define COMPILING_X_DLL only in that DLL's project. This method can be
used by multiple DLLs, a big advantage over AFX_EXT_CLASS, and since the
macro names are unique, they don't conflict. Then you can do things like:
X_EXPORT void f();
class X_EXPORT MyClass
{
...
};
Look in the project's preprocessor options, and you may find the AppWizard
has already defined a suitable COMPILING_X_DLL macro for you.
--
Doug Harrison
Visual C++ MVP