469,140 Members | 1,403 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,140 developers. It's quick & easy.

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

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
4 4301
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
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

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
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.

Similar topics

2 posts views Thread by Peter Albertsson | last post: by
3 posts views Thread by Aryeh M. Friedman | last post: by
10 posts views Thread by Rick Anderson | last post: by
8 posts views Thread by Paw Pedersen | last post: by
1 post views Thread by Alok Kumar | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.