Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A warning with building 64bit dll

dll export header

extern "C"
void _declspec(dllexport) __stdcall foo();

.def file

EXPORTS
foo         @1

When I build the dll by 64bit build config, I meet this warning.

warning LNK4197: export 'foo' specified multiple times; using first specification

But If I build the dll by 32bit build config, the warning never occurs.
What is the problem? What is the difference.

In dll header for interface, we usually use this technic,

#ifdef EXPORT_DLL
#define BASICAPI _declspec(dllexport)
#else
#define BASICAPI _declspec(dllimport)
#endif //_EXPORT_DLL

But if def file also exists, we always will be meet the warning when we are building 64bit dll.
So, should we write the codes like this?

#ifdef EXPORT_DLL
#define BASICAPI
#else
#define BASICAPI _declspec(dllimport)
#endif //_EXPORT_DLL

It works well. But it's not familiar to me.
Give me any your opinions.

like image 955
Benjamin Avatar asked Aug 26 '10 05:08

Benjamin


Video Answer


1 Answers

It's generally not good practise to specify exports twice for the same function. If you already have __declspec(dllexport) then you do not need to specify the export in a .def file as well. Conversely, if you have the export listed in a .def file, then there's no need for a __declspec(dllexport).

I believe the reason for the warning is that in x86 builds, the __declspec(dllexport) is exporting the decorated name with a leading underscore, but the 64-bit compiler does not decorate names with a leading underscore, leading to the duplicate. To verify this, you could look at the 32-bit DLL in Dependency Walker and you should see two exported functions, "foo" and "_foo".

like image 122
Dean Harding Avatar answered Oct 18 '22 12:10

Dean Harding