Exporting function in VS2008 DLL

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hi,

I am picking up some differences between VS2003 and VS2008/05's C++ compiler
in handling export functions in unmanaged DLL and am wondering if this is a
change in the two compilers?

This is a brief description of my sample project. I have a Foo.h which
contains this:
//Foo.h
#ifdef BUILD_MYDLL
#define DLLENTRY __declspec(dllexport)
#else
#define DLLENTRY __declspec(dllimport)
#endif

extern "C" { DLLENTRY int Foo(LPCTSTR); }

Then the definition of Foo() is defined in Foo.cpp as follows:
// in my Foo.cpp
#include "stdafx"
#include "Foo.h"

int Foo(LPCTSTR) { return 0; }

Now if I build this project in VS2003, I can see the export of Foo() in
unmangled name without using .DEF.

If I build this very same project in VS2008/05, I see no export function
unless I include the dllexport in the function definition as follows:

__declspec(dllexport)
int Foo(LPCTSTS) { return 0; }

This URL seems to explain why:
http://msdn.microsoft.com/en-us/library/da6zd0a4(VS.80).aspx
which says that:
To make the function part of the DLL interface, you must declare the
definition of the function in the other module as dllexport. Otherwise, a
linker error is generated when the client is built.

But in VS2003, I do not have to include the dllexport in function definition
in Foo.cpp. If I include __declspec(dllexport) in the definition of Foo() in
Foo.cpp in VS2005/08, I get the mangled exported name, despite my extern "C"
linkage. Then I need the .def file to get the unmangled name. Something I did
not have to do in VS2003.

Has anyone encounterd this kind of differences? Or is it the compiler just
happens to be tightening up the rules in this area?

To summarise, this appears to work on all 3 compilers:
1) If you want mangled name export, include __declspec(dllexport) in
function definition. Inclusion of __declspec(dllexport) in prototype is
insufficient.
2) If you want unmangled exported name, always use .DEF.

The change seems to have caught be by surprise.

Thanks.

.



Relevant Pages

  • Re: strange function syntax
    ... > device libraries have a function definition that looks like this: ... > (everything works fine w/o optimization). ... This could be a compiler problem ...
    (comp.lang.c)
  • Re: What is going on here?
    ... In article, Karl Heinz Buchegger ... > with the function definition, for the compiler it has not. ... > empty statement. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Doubt
    ... You are using the so-called "old style" function definition without a ... declaration list specifying the types of the parameters. ... If you had invoked your compiler in conforming mode then it would have. ... C99 conformance is incomplete. ...
    (comp.lang.c)
  • Re: Function with unspecified number of arguments
    ... int main ...     return 0; ... you just don't tell the compiler how many. ... you wrote a function definition: ...
    (comp.lang.c)