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

Discussion in 'C++' started by kk_oop@yahoo.com, Apr 7, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. Ian Collins Guest

    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.
     
    Ian Collins, Apr 7, 2006
    #2
    1. Advertising

  3. Rolf Magnus Guest

    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.
     
    Rolf Magnus, Apr 7, 2006
    #3
  4. Guest

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

    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, 2006
    #4
  5. red floyd Guest

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

    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.
     
    red floyd, Apr 7, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. A
    Replies:
    5
    Views:
    3,484
    Victor Bazarov
    Nov 27, 2003
  2. Thomas Matthews
    Replies:
    5
    Views:
    2,554
    tom_usenet
    Aug 2, 2004
  3. =?ISO-8859-1?Q?Martin_H=F6ller?=

    Method pointers / callbacks

    =?ISO-8859-1?Q?Martin_H=F6ller?=, Jun 12, 2005, in forum: C++
    Replies:
    8
    Views:
    498
    Maxim Yegorushkin
    Jun 12, 2005
  4. Michael Oswald

    static functions for callbacks

    Michael Oswald, Dec 5, 2007, in forum: C++
    Replies:
    5
    Views:
    371
    Michael Oswald
    Dec 6, 2007
  5. Andre
    Replies:
    5
    Views:
    565
    Keith Thompson
    Jul 17, 2012
Loading...

Share This Page