I was poking around the SDL 2 header files, and found that the majority of functions that I encountered had this form:
extern DECLSPEC int SDLCALL SDL_FunctionName();
I understand what extern
, int
, and SDL_FunctionName
all mean (the storage class specifier, return value, and function name, respectively). However, I must admit that I have not seen the likes of DECLSPEC
and SDLCALL
before. Searches for the former merely yield a Win32/64 API thing, and nothing good comes up for the latter.
What do these two things mean, and what do they do?
These are constants defined to change the export behavior of a symbol (the function) by the C/C++ compiler.
DECLSPEC
is a macro used in C/C++ header files. It stands for either __declspec(dllexport)
or __declspec(dllimport)
depending on whether you are the project library or the consumer. See here for more details.
In the declaring/owning project of the class, DECLSPEC
resolves to __declspec(dllexport)
. This tells the compiler to allow access to these functions from outside the DLL. It also changes the default naming behavior of the compiler (called name decoration or name mangling) to allow easier access to these symbols from outside the DLL. See here.
In the consuming project, DECLSPEC
resolves to __declspec(dllimport)
. This is not strictly necessary, but its use allows the compiler to generate more efficient code for the consumed symbols, such as in generation of thunks. See here.
SDLCALL
would be a constant which declares the calling convention of the function. You can read more about calling conventions such as __stdcall
, __cdecl
, etc. here.
These macros are used to make the program portable across many different architectures and platforms, just by redefining the constants. Usually, the compiler or SDK used on the platform has these macros globally defined.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With