On Fri, 25 Jul 2003 18:22:21 +0200, Rolf Magnus <> wrote:
>Eric Entressangle wrote:
>
>> Hi all,
>>
>> is there any trouble setting an C++ static class method as callback
>> function for a C program or library ?
>
>Yes. The function will have C++ calling conventions, but be called using
>C calling conventions. An some (many?) compilers, they are both the
>same, but if they differ, it won't work. For maximum portability, you
>should only use extern "C" functions for that, which is of course not
>possible for member functions, even if static.
Using an 'extern "C"' function doesn't buy additional portability...
Reason: neither C nor C++ defines the machine-level calling convention,
nor name-mangling or other relevant things. With at least one of the
most used C and C++ compilers 'extern "C"' has _no_ effect on the
calling convention whatsoever. What it does influence with that
compiler is the name mangling, i.e. linkage compatibility: the compiler
trusts you to know what you're doing, since this spec removes type
information (e.g. name mangling) in order to be linkage-compatible
with C.
The main assumption for the callback problem is calling convention
compatibility, and linkage compatibility doesn't enter the picture, but
the details of the calling convention compatibility depends on the
compilers, and one should not assume that 'extern "C"' will influence
calling conventions, data type compatibility, or any such thing.