Zebrafish
Zebrafish

Reputation: 14320

Do I need to prefix this function with __stdcall?

When learning about debugging OpenGL I implemented a function callback which would receive any debug error messages from the OpenGL API whenever something went wrong. In the tutorial it said that the function signature was:

typedef void APIENTRY funcname(GLenum source​, GLenum type​, GLuint id​,
   GLenum severity​, GLsizei length​, const GLchar* message​, const void* userParam​);

So on Windows I implemented this callback. On Windows APIENTRY is a define for __stdcall. __stdcall I believe is a Windows-specific keyword specifying the calling convention. Later I ported my code to Linux, and for starters my GCC with Eclipse didn't recognise APIENTRY, because it's a Windows define. So I changed it to __stdcall, which I'm not sure if it recognised or not, but regardless it threw an error saying:

"Expected initialiser before glCheckError_"

As my callback function is void __stdcall glCheckError_(/Params/). Removing the __stdcall preface makes the program work fine without it.

I'm wondering whether this prefix is necessary, for either Windows or Linux? The funny thing is that the place that suggested I add __stdcall to the function signature was the Khronos webpage, which holds documentation for OpenGL, so as far as I can tell it shouldn't be specifying OS-specific information, as OpenGL is cross-platform. So do I need this __stdcall prefix?

Upvotes: 1

Views: 1588

Answers (1)

robthebloke
robthebloke

Reputation: 9668

On windows, and only under 32bit, then it will make a difference. (The default is __cdecl calling convention, v.s. the __stdcall used by openGL). This can end up corrupting the stack if you use the wrong convention (the compiler should error with any luck).

On 64bit windows, it makes no difference (because stdcall isn't available, so all __stdcall/__cdecl will default to __fastcall).

It make no difference to linux/macos/android/ios.

Wrapping this in a macro should be all that's needed.

#if definded(_WIN32) && !defined(_WIN64)
# define STDCALL __stdcall
#else 
# define STDCALL 
#endif

Upvotes: 3

Related Questions