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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • AL@TW

    #1

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

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

    #2
    Re: How to call a C++ function from c code?

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

    Comment

    • Stephen Sprunk

      #3
      Re: How to call a C++ function from c code?

      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

      Comment

      • Peter Nilsson

        #4
        Re: How to call a C++ function from c code?

        "AL@TW" <schosnab...@gm ail.comwrote:
        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

        Comment

        • Keith Thompson

          #5
          Re: How to call a C++ function from c code?

          CBFalconer <cbfalconer@yah oo.comwrites:
          "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.c om/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_Keit h) kst-u@mib.org <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"

          Comment

          • Ian Collins

            #6
            Re: How to call a C++ function from c code?

            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.

            Comment

            • Richard Heathfield

              #7
              Re: How to call a C++ function from c code?

              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>

              --
              Richard Heathfield <http://www.cpax.org.uk >
              Email: -http://www. +rjh@
              Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
              "Usenet is a strange place" - dmr 29 July 1999

              Comment

              • Ian Collins

                #8
                Re: How to call a C++ function from c code?

                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.

                Comment

                • Malcolm McLean

                  #9
                  Re: How to call a C++ function from c code?


                  "AL@TW" <schosnablue@gm ail.comwrote in message
                  How to call a C++ function from c code?
                  extern "C++"?
                  >
                  Three ways:

                  1) extern "C" the C++ function in C++ code. If you don't want to touch the
                  C++ source, write a wrapper.

                  2) Compile the C code under C++ and call in the normal way. Note this means
                  you'll have to be careful not to use any C-only constructs, like uncast
                  malloc().

                  3) Examine the object file that the C++ compiler creates and call the
                  mangled function names that it outputs. This is the worst option but
                  cometimes necessary.


                  --
                  Free games and programming goodies.


                  Comment

                  • CBFalconer

                    #10
                    Re: How to call a C++ function from c code?

                    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.


                    Comment

                    • Greg Comeau

                      #11
                      Re: How to call a C++ function from c code?

                      In article <910db35a-ad6e-481b-8203-fc70d5102e5c@a3 g2000prm.google groups.com>,
                      AL@TW <schosnablue@gm ail.comwrote:
                      >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.
                      Check out http://www.comeaucomputing.com/techtalk/#externc
                      --
                      Greg Comeau / 4.3.10.1 with C++0xisms now in beta!
                      Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
                      World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
                      Comeau C/C++ with Dinkumware's Libraries... Have you tried it?

                      Comment

                      • Keith Thompson

                        #12
                        Re: How to call a C++ function from c code?

                        CBFalconer <cbfalconer@yah oo.comwrites:
                        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_Keit h) kst-u@mib.org <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"

                        Comment

                        • Kenny McCormack

                          #13
                          Re: How to call a C++ function from c code?

                          In article <lnabfuc65e.fsf @nuthaus.mib.or g>,
                          Keith Thompson <kst-u@mib.orgwrote:
                          >CBFalconer <cbfalconer@yah oo.comwrites:
                          >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).

                          Comment

                          Working...