Page 1 of 1

Calling conventions on callbacks

Posted: 19 Jul 2004 17:22
by matthijs
Hey,

I was playing around with Celestar's multistop patch, trying to compile it under msvc6. After some issues with variable declaration(*) I encountered an issue with calling conventions for callback functions.
Specifically, the vc project is now set to declare all functions as __fastcall. But, in Celestar's code, qsort is used, which expects it's callback to be of __cdecl convention.
I see two obvious solutions to this:
1. Change the project options to use __cdecl by default (I think this is msvc default). Downside to this would be speed, at least I assume __fastcall is faster?
2. Declare all callbacks with an explicit __cdecl. Since __cdecl is only understood by msvc (at least not by gcc), we would need a macro in the form of:

Code: Select all

#ifdef MSVC
    #define CALLBACK __cdecl
#else
    #define CALLBACK
#endif
(...)
int CALLBACK function(void*, void*) {
    (...)
}
I vote for the last one, which do you think is best?

Matthijs

(*) When you declare variables, you should only do it at the beginning of a scope (ie any {} block). If you declare a variable after a normal statement, msvc will barf.

Posted: 19 Jul 2004 17:59
by matthijs
How's this for detecting that MSVC is used? Is there a better way?

Code: Select all

#if defined(WIN32) && !defined(MINGW)
Matthijs

Posted: 20 Jul 2004 17:19
by matthijs
I just saw that there is already a macro called CDECL that would do the trick. Probably just use that one then.

Matthijs

Posted: 22 Jul 2004 21:35
by msalters
Too bad we're stuck with C and qsort, C++ std::sort is 6 times faster (due to inlined callbacks). Anyway, the classic trick to detect MSVC is to test _MSC_VER. IIRC, MSVC6 has _MSC_VER==1200, VC7.0 has 1300 and VC7.1 has 1301. I may be off by a few.