Reputation: 2736
Let's say my dll contains the following:
mydll.h
extern "C" void __declspec(dllexport) __stdcall f();
Usually in my app when performing static dll linking I just include the header as it is, but reading a bit about dllexport vs dllimport specifications I wonder if my app should be using the following prototype instead:
extern "C" void __declspec(dllimport) __stdcall f();
I'm using C++ Builder XE6 for both dll and app.
So, what is the proper way to do it?
Upvotes: 0
Views: 192
Reputation: 4245
Most libraries use the preprocessor to do this for you.
They contain a define for __declspec(dllimport/export)
#ifdef MYDLL_EXPORTS
#define MYDLL_API __declspec(dllexport)
#else
#define MYDLL_API __declspec(dllimport)
#endif
void MYDLL_API __stdcaall f();
When the library creator builds the DLL, they define MYDLL_EXPORTS
and export the functions, when a client includes the header that isn't defined and they will import the functions.
Upvotes: 2
Reputation: 47952
If your DLL exports only undecorated C functions (extern "C"
), then you can skip all the non-standard attributes and use a .DEF file to export the necessary functions. This is a nice option if your DLL offers a small number of C-compatible APIs.
In theory, you can use a .DEF file with decorated functions (including C++ member functions), but it's not usually worth the effort. You first have to figure out exactly how each function was decorated (mangled) and then manually add those to the .DEF file.
If you want to export C++ interfaces, then you need to use the both __declspec(dllexport)
and __declspec(dllimport)
as shown in the other answers.
Upvotes: 0
Reputation: 206567
You'll have to use macros to make sure that you use:
extern "C" void __declspec(dllexport) __stdcall f();
when you build the DLL and
extern "C" void __declspec(dllimport) __stdcall f();
when using the DLL.
See What does this c++ class declaration mean? for some ideas.
Upvotes: 2