Calling C++ functions from C

Discussion in 'C++' started by Praveen Srinivasan, Aug 18, 2003.

  1. Hi all,
    Are there any important issues that one should be aware of when calling C++
    functions from C? In particular, I'm passing a function pointer to a C
    routine in a library, and in that function I do C++ type stuff, e.g.
    utilizing operator overloading. Is that reasonable, or are there some
    pitfalls I need to watch out for?

    Thanks,
    Praveen Srinivasan
     
    Praveen Srinivasan, Aug 18, 2003
    #1
    1. Advertising

  2. Praveen Srinivasan

    Attila Feher Guest

    Praveen Srinivasan wrote:
    > Hi all,
    > Are there any important issues that one should be aware of when
    > calling C++ functions from C? In particular, I'm passing a function
    > pointer to a C routine in a library, and in that function I do C++
    > type stuff, e.g. utilizing operator overloading. Is that reasonable,
    > or are there some pitfalls I need to watch out for?


    Are you doing callbacks?

    You will not be able to pass member function pointers (C has no idea of
    them). But I guess you know that. Another thing to remember is that as
    soon as you do not pass pointers but call by name from C, you will need to
    make the function extern "C" to get rid of the name mangling.

    A third issue (unfortunately not important in most of todays C++ code) is
    that it is very unhealthy to let the C++ exceptions escape from your C++
    function into C! So if you have ANY chance of exceptions either make the
    function throw() (so C++ will stop the app) or catch them all and ignore
    them (and return error to the caller???).

    A
     
    Attila Feher, Aug 18, 2003
    #2
    1. Advertising

  3. Praveen Srinivasan

    Noah Roberts Guest

    Attila Feher wrote:
    > Praveen Srinivasan wrote:
    >
    >>Hi all,
    >>Are there any important issues that one should be aware of when
    >>calling C++ functions from C? In particular, I'm passing a function
    >>pointer to a C routine in a library, and in that function I do C++
    >>type stuff, e.g. utilizing operator overloading. Is that reasonable,
    >>or are there some pitfalls I need to watch out for?

    >
    >
    > Are you doing callbacks?
    >
    > You will not be able to pass member function pointers (C has no idea of
    > them).


    I did not think it was possible to get member function pointers. How is
    it done. It would certainly be easier than functors or multiple
    inheritance.

    NR
     
    Noah Roberts, Aug 18, 2003
    #3
  4. Attila Feher wrote:

    > Praveen Srinivasan wrote:
    >
    >>Hi all,
    >>Are there any important issues that one should be aware of when
    >>calling C++ functions from C? In particular, I'm passing a function
    >>pointer to a C routine in a library, and in that function I do C++
    >>type stuff, e.g. utilizing operator overloading. Is that reasonable,
    >>or are there some pitfalls I need to watch out for?

    >
    >
    > Are you doing callbacks?
    >
    > You will not be able to pass member function pointers (C has no idea of
    > them). But I guess you know that. Another thing to remember is that as
    > soon as you do not pass pointers but call by name from C, you will need to
    > make the function extern "C" to get rid of the name mangling.


    extern "C" does more than turn off name mangling, I think. I believe you
    need it even when you are calling via a pointer to ensure the proper
    calling conventions.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
     
    Kevin Goodsell, Aug 18, 2003
    #4
  5. Praveen Srinivasan

    Attila Feher Guest

    Noah Roberts wrote:
    > Attila Feher wrote:
    >> Praveen Srinivasan wrote:
    >>
    >>> Hi all,
    >>> Are there any important issues that one should be aware of when
    >>> calling C++ functions from C? In particular, I'm passing a function
    >>> pointer to a C routine in a library, and in that function I do C++
    >>> type stuff, e.g. utilizing operator overloading. Is that reasonable,
    >>> or are there some pitfalls I need to watch out for?

    >>
    >>
    >> Are you doing callbacks?
    >>
    >> You will not be able to pass member function pointers (C has no idea
    >> of them).

    >
    > I did not think it was possible to get member function pointers. How
    > is it done. It would certainly be easier than functors or multiple
    > inheritance.


    It is possible, but it is rarely necessary. And as I have said: it is not
    something compatible with C. Think of the member function pointer as a
    "number" telling: if this mfptr is applied together with a proper object,
    call function 4. That function 4 can be virtual etc. So it is not really a
    pointer in the conventional sense of being an address to the first (machine
    code) statement of a function.

    --
    Attila aka WW
     
    Attila Feher, Aug 19, 2003
    #5
  6. Praveen Srinivasan <> wrote in message news:<bhqbbu$5c5$>...
    > Hi all,
    > Are there any important issues that one should be aware of when calling C++
    > functions from C? In particular, I'm passing a function pointer to a C
    > routine in a library, and in that function I do C++ type stuff, e.g.
    > utilizing operator overloading. Is that reasonable, or are there some
    > pitfalls I need to watch out for?
    >
    > Thanks,
    > Praveen Srinivasan


    You'll need to compile the file that contains main() with the C++ compiler.
     
    David Cattarin, Aug 19, 2003
    #6
    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. Ryan Ternier
    Replies:
    4
    Views:
    4,014
    Cowboy \(Gregory A. Beamer\) [MVP]
    Jul 29, 2004
  2. Xiangliang Meng
    Replies:
    1
    Views:
    1,629
    Victor Bazarov
    Jun 21, 2004
  3. Daniel Vallstrom
    Replies:
    2
    Views:
    1,942
    Kevin Bracey
    Nov 21, 2003
  4. S?ren Gammelmark
    Replies:
    1
    Views:
    1,929
    Eric Sosman
    Jan 7, 2005
  5. Ross A. Finlayson
    Replies:
    19
    Views:
    622
    Keith Thompson
    Mar 10, 2005
Loading...

Share This Page