Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do DECLSPEC and SDLCALL mean in a function definition in a header file on Linux mean?

Tags:

c

linux

sdl

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?

like image 507
fouric Avatar asked Dec 25 '22 09:12

fouric


1 Answers

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.

  1. 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.

  2. 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.

like image 118
metacubed Avatar answered Apr 13 '23 01:04

metacubed