Calling Conventions for Libraries on Mac and UNIX

Started by
10 comments, last by Markie 13 years, 4 months ago
Quote:Original post by Markie
Quote:Original post by SimonForsman
In general you want __stdcall for dynamic libraries yes since those functions won't be inlined anyway allthough the only real benefit is marginally smaller code and compatibility with languages that doesn't support __cdecl (such as VB6), i was wrong in my earlier post though, gcc and msvc doesn't handle __stdcall in the same way. for gcc you want:
__attribute__((stdcall)) rather than __stdcall

so you should probably add:

#ifdef __GNUC__
#define __stdcall __attribute__((stdcall))
#endif

or better yet i guess:

#ifndef WIN32
#define __stdcall
#endif

then simply ignore all non Microsoft compilers for the Windows platform and let other platforms use their default convention.

IIRC this applies to gcc on all platforms, even Windows so its necessary if you want to support more than one compiler.


Hey yea, thanks!
That was exactly my next question, I was gonna add it by edit, but you've already addressed it: :-)
The implementations of invoking the std and cdecl calling conventions are not the same in Windows and in POSIX, are they?
If they are not, as your posts suggests (on Linux, std calling convention is invoked by "__attribute__((stdcall))" instead of "__stdcall", it might be wise to create cross-platform (cross-compiler as well?) macros such as "cp_CALLCONV_STD" and "cp_CALLCONV_CDECL" which map to std and cdecl with the correct implementation on every platform / compiler.
For instance:
*** Source Snippet Removed ***

I prefer a "complicated" setup like this instead of a "nifty" #ifndef on one platform only. This way I can explicitly SET any calling convention I want for any function on each platform with one single macro. And if I omit the macro, I simply get the native convention.
(That would probably be in line with your probably good suggestion of using the calling convention which best suits you personally for every function call, regardless of platform or any possible traditions, etc. :-)

For you Linux gurus out there, I'd be curious what the "attribute" stands for in __attribute__((stdcall))?
Does it have to be replaced with anything (if so, what?) or is that literally how std calling convention is invoked? If so what's the meaning / background of "attribute"??
:-)

Mark


__stdcall and __attribute are compiler specific extensions, so __stdcall is unique to msvc (other compilers may or may not support it though), gcc on the other hand uses __attribute__((*)) for all function attributes where * is the function attribute you wish to specify.

With gcc calling conventions are set using __attribute__((cdecl)) or __attribute__((stdcall)) , but you can also set deprecated status on functions using for example __attribute__((deprecated("Some Warning Message"))) or __attribute__((pure)) for pure functions (can improve code optimization). (There are also function attributes for hot and cold functions and much more that will help the optimizer when doing non PGO builds)

a complete list of gcc function attributes can be found here:
http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html

If your goal is crossplatform i'd recommend either going with cdecl (since its the default convention for c and c++ and thus doesn't require any compiler specific configuration) all the way or restricting yourself to one or two compilers per platform. (If anyone wants to use a compiler you're not supporting they can probably configure aliases for __stdcall etc themselves)
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
Advertisement
Hey thanks SimonForsman! Awesome!
:-)))

This topic is closed to new replies.

Advertisement