Using C++ functions from C

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • MSG

    Using C++ functions from C

    Hello

    The C++ standard provides a way to access C function, which is `extern
    "C"`, however, what about the compatibility in the opposite direction?

    If my C++ function happens to have the signature of a valid C
    function, are there guarantees about its usability from C?

    Thanks
    MSG
  • DeMarcus

    #2
    Re: Using C++ functions from C



    MSG wrote:[color=blue]
    > Hello
    >
    > The C++ standard provides a way to access C function, which is `extern
    > "C"`, however, what about the compatibility in the opposite direction?
    >
    > If my C++ function happens to have the signature of a valid C
    > function, are there guarantees about its usability from C?
    >
    > Thanks
    > MSG[/color]

    I haven't investigated it thoroughly, but try enclose your C++ functions
    within extern "C" {} as well. Of course it has to be static functions,
    but they in turn can use C++ fully.

    Regards
    Daniel Marcus




    Comment

    • David Harmon

      #3
      Re: Using C++ functions from C

      On 9 Feb 2004 02:48:31 -0800 in comp.lang.c++, msg1825@yahoo.c om (MSG)
      was alleged to have written:[color=blue]
      >The C++ standard provides a way to access C function, which is `extern
      >"C"`, however, what about the compatibility in the opposite direction?
      >
      >If my C++ function happens to have the signature of a valid C
      >function, are there guarantees about its usability from C?[/color]

      Not "just happens to" but you have declared it have "C" linkage with
      extern "C"

      This issue is covered in Marshall Cline's C++ FAQ. See the topic
      "[32.6] How can I create a C++ function f(int,char,floa t) that is
      callable by my C code?" It is always good to check the FAQ before
      posting. You can get the FAQ at:


      Comment

      • Evan Carew

        #4
        Re: Using C++ functions from C

        MSG,

        If you need an example project which accomplishes this task, drop me a line.

        Evan Carew

        MSG wrote:[color=blue]
        > Hello
        >
        > The C++ standard provides a way to access C function, which is `extern
        > "C"`, however, what about the compatibility in the opposite direction?
        >
        > If my C++ function happens to have the signature of a valid C
        > function, are there guarantees about its usability from C?
        >
        > Thanks
        > MSG[/color]

        Comment

        • Dylan Nicholson

          #5
          Re: Using C++ functions from C

          DeMarcus <nobody@tellus. orb> wrote in message news:<40277462. 4070208@tellus. orb>...[color=blue]
          > MSG wrote:[color=green]
          > > Hello
          > >
          > > The C++ standard provides a way to access C function, which is `extern
          > > "C"`, however, what about the compatibility in the opposite direction?
          > >
          > > If my C++ function happens to have the signature of a valid C
          > > function, are there guarantees about its usability from C?
          > >
          > > Thanks
          > > MSG[/color]
          >
          > I haven't investigated it thoroughly, but try enclose your C++ functions
          > within extern "C" {} as well. Of course it has to be static functions,
          > but they in turn can use C++ fully.
          >[/color]

          Hmm...I'm unsure as to the exact meaning of extern "C" { static void
          foo(); } - you're telling the compiler two things at once (firstly
          that you want it to have external "C"-style linkage, secondly that you
          want it not to be visible outside the current compilation unit). Note
          that extern "C" is not valid on a struct/class member, even if it is
          'static'. And

          extern "C" static void foo();

          is not a valid declaration.

          Dylan

          Comment

          • DeMarcus

            #6
            Re: Using C++ functions from C



            Dylan Nicholson wrote:
            [color=blue]
            > DeMarcus <nobody@tellus. orb> wrote in message news:<40277462. 4070208@tellus. orb>...
            >[color=green]
            >>MSG wrote:
            >>[color=darkred]
            >>>Hello
            >>>
            >>>The C++ standard provides a way to access C function, which is `extern
            >>>"C"`, however, what about the compatibility in the opposite direction?
            >>>
            >>>If my C++ function happens to have the signature of a valid C
            >>>function, are there guarantees about its usability from C?
            >>>
            >>>Thanks
            >>>MSG[/color]
            >>
            >>I haven't investigated it thoroughly, but try enclose your C++ functions
            >>within extern "C" {} as well. Of course it has to be static functions,
            >>but they in turn can use C++ fully.
            >>[/color]
            >
            >
            > Hmm...I'm unsure as to the exact meaning of extern "C" { static void
            > foo(); } - you're telling the compiler two things at once (firstly
            > that you want it to have external "C"-style linkage, secondly that you
            > want it not to be visible outside the current compilation unit). Note
            > that extern "C" is not valid on a struct/class member, even if it is
            > 'static'. And
            >
            > extern "C" static void foo();
            >
            > is not a valid declaration.
            >
            > Dylan[/color]


            I made a small program like this

            class MyCppClass
            {
            public:
            char* helloWorldMessa ge( void ) { return "Hello World"; }
            };

            extern "C"
            {

            char* foo( void )
            {
            MyCppClass cpp;

            return cpp.helloWorldM essage();
            }

            }

            It seems to work, but I'm curious in what the common way
            to solve this task is.

            Daniel


            Comment

            • EventHelix.com

              #7
              Re: Using C++ functions from C

              You cannot do that as a C++ method maps to a C function with an additional
              pointer that points to the object.

              See the following article for details:



              Sandeep
              --
              Sequence diagram based systems engineering and architecture design tool. Built in support for alternative scenarios and multi-tier architectures.

              EventStudio 2.0 - Distributed System Design CASE Tool

              Comment

              Working...