By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,433 Members | 776 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,433 IT Pros & Developers. It's quick & easy.

"C" Callbacks: Static method vs. extern "C"

P: n/a
Hi. I need to write a C++ callback function and register it with a C
program. I've read that this can be done if the callback function is a
static method. I've also read that I should use a global function with
the extern "C" prefix. I was leaning toward using the static method
approach until I saw a posting that said compatibility between static
C++ functions and C is not guaranteed in the respective language
standards. This made me start to favor the extern C approach.

Any thoughts?

Thanks!

Ken

Apr 7 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
kk****@yahoo.com wrote:
Hi. I need to write a C++ callback function and register it with a C
program. I've read that this can be done if the callback function is a
static method. I've also read that I should use a global function with
the extern "C" prefix. I was leaning toward using the static method
approach until I saw a posting that said compatibility between static
C++ functions and C is not guaranteed in the respective language
standards. This made me start to favor the extern C approach.

Any thoughts?

You're heading in the right direction!

A static method will probably work, an extern "C" function will
definitely work.

Some compilers will emit a warning if you don't pass an extern "C"
function where one is expected.

--
Ian Collins.
Apr 7 '06 #2

P: n/a
kk****@yahoo.com wrote:
Hi. I need to write a C++ callback function and register it with a C
program. I've read that this can be done if the callback function is a
static method. I've also read that I should use a global function with
the extern "C" prefix. I was leaning toward using the static method
approach until I saw a posting that said compatibility between static
C++ functions and C is not guaranteed in the respective language
standards. This made me start to favor the extern C approach.


Well, basically, none of them is really guaranteed. After all, the C++
standard doesn't require a C compiler to exist at all. However, often, the
C and C++ compilers are bundled together or made compatible. In that case,
a global extern "C" function is indeed the best choice. The thing is that
it selects "C" linkage, which is supposed to be the same linkage as the C
compiler uses. "Linkage" not only includes the functions internal symbol
names, but also the calling conventions, like how arguments are passed, who
cleans up the stack and so on. Those might be different in the C and C++
compilers, so their linkages could be incompatible.
The bottom line is that in many (probably most) compilers, it will work
either way, but the extern "C" approach is a bit more safe.

Apr 7 '06 #3

P: n/a

kk****@yahoo.com wrote:
Hi. I need to write a C++ callback function and register it with a C
program. I've read that this can be done if the callback function is a
static method. I've also read that I should use a global function with
the extern "C" prefix. I was leaning toward using the static method
approach until I saw a posting that said compatibility between static
C++ functions and C is not guaranteed in the respective language
standards. This made me start to favor the extern C approach.

Any thoughts?


FWIW I think that the following is legal and makes S::func use C
calling convention:

extern "C"{
struct S{
static int func(){return 1;}
};
}
regards
Andy Little

Apr 7 '06 #4

P: n/a
an**@servocomm.freeserve.co.uk wrote:
FWIW I think that the following is legal and makes S::func use C
calling convention:

extern "C"{
struct S{
static int func(){return 1;}
};
}


FWIW, Comeau online has no problem with the above snippet.
Apr 7 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.