function declaration with MACROs

hi guys as I know function declarations are like this:

 
  void ClearBackground(Color color); 


however sometimes I can face some kind of function declarations with macros like this:

 
  RLAPI void ClearBackground(Color color); 


even more complicated ones:

 
 _CRTIMP __cdecl   __MINGW_NOTHROW  FILE * fopen (const char *, const char *);


what do these MACROs mean???

and what MACRO can take below???

 
 MACRO  type name(parameters);


or is it possible to make some declaration like this:

 
 MACRO  MACRO  type name(parameters);


and what does this mean???
RLAPI is from the RayLib library, and is a specialized calling convention for the library.

https://www.reddit.com/r/learnprogramming/comments/dxnw3u/what_does_this_c_construct_in_the_raylib_library/

The file open is yet another example of specialized calling convention.

https://stackoverflow.com/questions/5016435/meaning-of-declaration-of-function

Note well that calling conventions like this are usually very implementation/platform specific.

The Windows Desktop API is chock full of platform specific calling conventions, as well as data types.

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int nWinMode)



I have even seen macros that do nothing at all used in front of functions as a weird type of comment/grouping. The only way to know what it does is either look it up (if a well known library) or back-track to the macro to see what it does (which may call even more macros!). Its perfectly legal to do something like
#define procedure void

procedure foo(int x); //great... its pascal!
or even stuff like
#define nada
nada int wtf(nada int bork); //yep, nothing happened here, but you had to figure it out to know that


5 min in the windows tools is like a crash course in why macros are generally bad (hard to read/hard to follow at best) but you can also learn some clever uses for them (as automatic switches to do different things based off compile time info).
Last edited on
Where there is extra info specified before the function name (apart from the return type), this extra info specifies the conventions for passing arguments and return values between functions and callers. The common ones for MS are __cdecl and __stdcall. These are often part of a macro definition. eg WINAPI is a macro for __stdcall. These are not part of the standard and can vary between compilers. If a function is defined as requiring a specific calling convention (eg for a callback function) then it's definition must also specify the same convention. These conventions specify how parameters are passed to/from the function and who cleans up the stack when the function returns.

For MS see:
https://learn.microsoft.com/en-us/cpp/cpp/argument-passing-and-naming-conventions?view=msvc-170
https://learn.microsoft.com/en-us/cpp/build/x64-calling-convention?view=msvc-170
https://learn.microsoft.com/en-us/cpp/cpp/microsoft-specific-modifiers?view=msvc-170
I do recall needing __declspec( dllimport ) and __declspec( dllexport ) no declarations that did interfare a (DLL) library. Building the library did require the export and code that did use the library did require the import. It was convenient to use macro in the headers. The macro was given value based on what we were compiling (and empty value on non-MS platforms).

Although, extern "C" prefix is used on non-MS too. https://en.cppreference.com/w/cpp/language/language_linkage
MS __declspec() can be used for more than specifying linkage for dll import/export. See:
https://learn.microsoft.com/en-us/cpp/cpp/declspec?view=msvc-170

extern "C" is used to prevent linker namespace 'mangling' when interfacing C++ code to non-C++. It specifies that for the specified functions c names are used for linking. A c link name just specifies the name whereas a c++ name also specifies the type of parameters using a lettering system to state how many parameters and to differentiate say double from int, int from int& etc. With a c interface you can't have external function overrides but you can with a C++ interface. See:
https://learn.microsoft.com/en-us/cpp/cpp/extern-cpp?view=msvc-170

Bottom line...

When running across code that has unfamiliar bits and pieces: spend time poking around the bowels of the interwebz. More often than not using the exact line of code as the search parameter pulls up info sufficient to Lucy 'splain what's going on.

That is what I did with the 2nd and 3rd code snippets. The 3rd was a bit more on-target than the RLAPI search, but I eventually "got there".

When I first started learning C/C++ on my own there were little to no web resources, only books. Self-teaching is so much easier now with multiple websites.

A good tutorial site for C++ that is kept up-to-date is Learn C++:

https://www.learncpp.com/

It's good for C++ basics, the more arcane features of newer standards are but briefly mentioned.
And, for one question within the OP:
Yes, there can be more than just type name(parameters); in a function declaration.
Topic archived. No new replies allowed.