How to call a C++ function from c code?

Discussion in 'C Programming' started by AL@TW, Aug 3, 2008.

  1. AL@TW

    AL@TW Guest

    How to call a C++ function from c code?
    extern "C++"?

    I think it just inhibit the warning.
    I maybe have to consider c++ object creation problem.

    Does the C++ need to be a static function?

    BTW, the c code is compiled with C++ compiler.
     
    AL@TW, Aug 3, 2008
    #1
    1. Advertising

  2. AL@TW

    CBFalconer Guest

    "AL@TW" wrote:
    >
    > How to call a C++ function from c code?
    > extern "C++"?


    No. In general, you can't, because of C++ overloading. You can
    call C from C++, however.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.
     
    CBFalconer, Aug 3, 2008
    #2
    1. Advertising

  3. AL@TW wrote:
    > How to call a C++ function from c code?
    > extern "C++"?
    >
    > I think it just inhibit the warning.
    > I maybe have to consider c++ object creation problem.


    This is OT for comp.lang.c; comp.lang.c++ is down the hall, to the left.

    <OT>
    The answer I assume you'll get is that the C++ function declaration
    should be enclosed in an extern "C" { ... } block when compiled in C++,
    while the prototype declaration that the C compiler sees should _not_ be
    so enclosed. The usual way to do this is with the BEGIN_C_DECLS and
    END_C_DECLS macros (Google for the idiomatic definitions) in a single
    header that can be used by compilers of both languages.

    I don't recall if the C++ compiler needs the function definition to be
    enclosed with extern "C" { ... } or whether it will "remember" to
    compile the function C-style simply by having the declaration enclosed.

    > Does the C++ need to be a static function?


    No. However, some features of C++ aren't available in a function that
    is being compiled as extern "C", like default arguments, variable
    signatures, etc. I'm pretty sure there's no way to do it with a class
    method either, whether static or not, just plain functions. A common
    tactic is to have a C-compatible "wrapper" function, which in turn calls
    the real C++ functions. (For instance, an object may be passed in to
    the wrapper via a void* parameter, and the wrapper then casts it to an
    object and invokes a method on the result.)
    </OT>

    > BTW, the c code is compiled with C++ compiler.


    If you're compiling it with a C++ compiler, the code is C++. You may
    have used a subset of C++ that looks the same as C (and hopefully has
    the same meaning, which isn't guaranteed), but it isn't C unless you're
    using a C compiler.

    S
     
    Stephen Sprunk, Aug 3, 2008
    #3
  4. "AL@TW" <> wrote:
    > How to call a C++ function from c code?


    You don't. [You can in certain cases, but the details
    are implementation specific and outside the scope of
    the C language itself, hence off-topic in comp.lang.c.]

    > extern "C++"?
    > I think it just inhibit the warning.
    > I maybe have to consider c++  object creation problem.
    >
    > Does the C++ need to be a static function?


    Ask about C++ in comp.lang.c++.

    > BTW, the c code is compiled with C++ compiler.


    Then you don't have a C language question, but a C++
    question. Again, ask in comp.lang.c++.

    --
    Peter
     
    Peter Nilsson, Aug 3, 2008
    #4
  5. CBFalconer <> writes:
    > "AL@TW" wrote:
    >> How to call a C++ function from c code?
    >> extern "C++"?

    >
    > No. In general, you can't, because of C++ overloading. You can
    > call C from C++, however.


    <OT>
    Yes, you can call C from C++, and you can call C++
    from C. This is explained in the "C++ FAQ Lite",
    <http://www.parashift.com/c++-faq-lite/>, section 32.

    Chuck, as I recall, last time we discussed this, you refused to
    believe it was possible to call C++ from C even after I demonstrated
    it.

    In any case, it happens to be C++, not C, that provides the
    mechanisms for calling functions in either language from the other,
    so this is a question a comp.lang.c++ -- but only if the C++ FAQ
    doesn't answer the question.

    But as Stephen Sprunk already pointed out, the OP isn't really trying
    to call C++ from C anyway; since he's compiling all is code with
    a C++ compiler, it's all C++ code, and there are no inter-language
    calling issues at all. (What the OP is already doing may well be
    the best approach anyway.)
    </OT>

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Aug 3, 2008
    #5
  6. AL@TW

    Ian Collins Guest

    CBFalconer wrote:
    > "AL@TW" wrote:
    >> How to call a C++ function from c code?
    >> extern "C++"?

    >
    > No. In general, you can't, because of C++ overloading.


    Not that old nonsense again. I though we ponded the truth into you last
    time around.

    --
    Ian Collins.
     
    Ian Collins, Aug 3, 2008
    #6
  7. AL@TW

    Ian Collins Guest

    Richard Heathfield wrote:
    > Ian Collins said:
    >
    >> CBFalconer wrote:
    >>> "AL@TW" wrote:
    >>>> How to call a C++ function from c code?
    >>>> extern "C++"?
    >>> No. In general, you can't, because of C++ overloading.

    >> Not that old nonsense again. I though we ponded the truth into you last
    >> time around.

    >
    > <croak>
    > Ribbit. Ribbit.
    > </croak>
    >

    :)

    --
    Ian Collins.
     
    Ian Collins, Aug 3, 2008
    #7
  8. AL@TW

    CBFalconer Guest

    Ian Collins wrote:
    > CBFalconer wrote:
    >> "AL@TW" wrote:
    >>
    >>> How to call a C++ function from c code?
    >>> extern "C++"?

    >>
    >> No. In general, you can't, because of C++ overloading.

    ^^^^^^^^^^^
    >
    > Not that old nonsense again. I though we ponded the truth into
    > you last time around.


    And I cavilled above.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.
     
    CBFalconer, Aug 3, 2008
    #8
  9. CBFalconer <> writes:
    > Ian Collins wrote:
    >> CBFalconer wrote:
    >>> "AL@TW" wrote:
    >>>
    >>>> How to call a C++ function from c code?
    >>>> extern "C++"?
    >>>
    >>> No. In general, you can't, because of C++ overloading.

    > ^^^^^^^^^^^
    >>
    >> Not that old nonsense again. I though we ponded the truth into
    >> you last time around.

    >
    > And I cavilled above.


    The problem is that the phrase "In general" is ambiguous here. I
    think what you meant is that it's not always possible, which is true.
    An equally reasonable reading is that it's *usually* not possible,
    which is not true. (Even in the presence of overloading, you can
    provide a uniquely named C++ wrapper.)

    In any case, saying that "In general, you can't" is hardly responsive
    to a question about how to do it.

    Yes, you certainly can call C++ from C. No, you can't always do it;
    there are cases where C++ features are incompatible with C, making a
    C-to-C++ call impossible or impractical. Since it happens to be C++,
    not C, that provides the mechanisms for cross-language calls in both
    directions, comp.lang.c++ is the place to ask about it, but only if
    the detailed information in the "C++ FAQ Lite" doesn't already answer
    the question.

    Yes, this is off-topic, but (a) I'm providing a redirection to a forum
    where it's topical, and (b) I'm also correcting a statement that's
    both off-topic and misleading.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Aug 3, 2008
    #9
  10. In article <>,
    Keith Thompson <> wrote:
    >CBFalconer <> writes:
    >> Ian Collins wrote:
    >>> CBFalconer wrote:
    >>>> "AL@TW" wrote:
    >>>>
    >>>>> How to call a C++ function from c code?
    >>>>> extern "C++"?
    >>>>
    >>>> No. In general, you can't, because of C++ overloading.

    >> ^^^^^^^^^^^
    >>>
    >>> Not that old nonsense again. I though we ponded the truth into
    >>> you last time around.

    >>
    >> And I cavilled above.

    >
    >The problem is that the phrase "In general" is ambiguous here. I


    But you of all people should know that Chuck's interpretation *is* the
    clc-approved one. People routinly say things like "That doesn't work in
    general" when it works perfectly well on 99.9999% of all known machines.

    And, mathematically speaking, they are right (to do so).
     
    Kenny McCormack, Aug 4, 2008
    #10
    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. grbgooglefan
    Replies:
    2
    Views:
    439
    Pascal Bourguignon
    Jan 30, 2008
  2. grbgooglefan
    Replies:
    4
    Views:
    458
    Kenny McCormack
    Jan 30, 2008
  3. grbgooglefan
    Replies:
    0
    Views:
    410
    grbgooglefan
    Jan 30, 2008
  4. Alok
    Replies:
    3
    Views:
    262
  5. THAKUR PRASHANT SINGH

    Class Function call vs Normal Function call

    THAKUR PRASHANT SINGH, Feb 26, 2010, in forum: Ruby
    Replies:
    7
    Views:
    203
    THAKUR PRASHANT SINGH
    Feb 27, 2010
Loading...

Share This Page