Re: extern "C", C++, and GTK+ callbacks.



Murray,

I believe you don't have much choice in the matter.  We've been bitten
by the same problem, and we came up with the same solution.

When gcc accepts the code, it is is silently converting an pointer to
function with C linkage to a pointer to function with C++ linkage. There is no reason, from a portability and standards point of view,
for the two to be the same.

As hard as it is for me to say, I believe that the SunPro compiler's
behavior is, in this case, correct.

Robert Zeh
http://home.earthlink.net/~rzeh

Murray Cumming wrote:

Some compilers [1], correctly complain when we use a static member
function as a GTK+ callback. GTK+ expects (and declares) an "extern C"
pointer-to-function, but our static member methods are not declared as
"extern C".

Unfortunately, it seems that static member methods can never be extern
"C". g++ does not allow anything like this:

class something
{
public:
extern "C" void function_one();
 extern "C"
 {
   void function_one();
 }
}


So we have to move the callbacks outside of the class, and declare them
as friend functions so that they can access the class:

extern "C"
{
 void Something_function_one();
}

class something
{
public:
 friend void function_one();

protected:
 void something_protected();
}

void Something_function_one()
{
 //Get something somehow.
 //Use it:
 something.something_protected();
}


This is annoying. Does anyone have a simpler suggestion?


[1] SUN Forte issues warnings. MipsPro issues errors, stopping the
compilation.





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]