Exporting function in VS2008 DLL
- From: Leon <newsgroup01.3.mailcenter101@xxxxxxxxxxxx>
- Date: Sun, 17 Aug 2008 19:26:00 -0700
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.
.
- Follow-Ups:
- Re: Exporting function in VS2008 DLL
- From: Alex Blekhman
- Re: Exporting function in VS2008 DLL
- From: Ulrich Eckhardt
- Re: Exporting function in VS2008 DLL
- From: Alessandro Vergani
- Re: Exporting function in VS2008 DLL
- Prev by Date: STA owner thread blocking issue
- Next by Date: proxy stub code
- Previous by thread: STA owner thread blocking issue
- Next by thread: Re: Exporting function in VS2008 DLL
- Index(es):
Relevant Pages
|