473,322 Members | 1,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

"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 4692
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Peter Albertsson | last post by:
Hi, I have a classname in a variable, and I whish to call a static method on that class, what's the syntax for that? I'm using PHP5. Eg. <?php
3
by: Aryeh M. Friedman | last post by:
Why is the value of "Instance" whacked in the following code. Note this only happens in Graph::*Wrapper. Also please note that if I hardcode WalkFunc and PrintFunct to be BredthFirst and...
10
by: Rick Anderson | last post by:
All, I am receiving the following compilation error on LINUX (but not Solaris, HPUX, WIN32, etc): compiling osr.c LBFO.h(369): warning #64: declaration does not declare anything extern...
8
by: Paw Pedersen | last post by:
Is there a way to save a variabel that can be access from a static method? I hope there would be some way to save it in memory so I don't have to save it in a file. It's only for a few minutes the...
1
by: Daniel | last post by:
If my System.IO.StreamWriter Write method throws "The specified network name is no longer available." and I try to Dispose or Close it in the finaly clause the close or dispose method just throws...
5
by: none | last post by:
I'd like to create a new static property in a class "hiding" the property present in a base class. Since this needs to happen at runtime I tried doing this via DynamicMethod. But obviously the...
1
by: Alok Kumar | last post by:
a.cxx extern int b(); static int a(); int main() { b(); return a(); } static int a(){return 1;};
5
by: DamienS | last post by:
Hi, I have a static method in a class and I need to be able to return a reference to "this". Googling around, I found a heap of discussions of the pros/cons of "abstract static" etc. It was...
1
by: Anouar Seljouki | last post by:
1) I built and application that I called contact here is the script for it using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace Contacts...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.