call c++ function from c

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • falk.von.roetel@web.de

    call c++ function from c

    Hello,

    I have the problem to call a c++ funtion in a C project. I have a
    header file from the cpp file. In the header file the function is
    decleared "int GetHostIPAddres ses(ini i);". I inlcude the header file
    in the c file and call the function GetHostIPAddres ses(1);. But the
    linker doesn't found the function. Can someone give me a hint what is
    wrong.

    Best regards

    Falk

  • Jens Thoms Toerring

    #2
    Re: call c++ function from c

    falk.von.roetel @web.de wrote:
    I have the problem to call a c++ funtion in a C project. I have a
    header file from the cpp file. In the header file the function is
    decleared "int GetHostIPAddres ses(ini i);". I inlcude the header file
    in the c file and call the function GetHostIPAddres ses(1);. But the
    linker doesn't found the function. Can someone give me a hint what is
    wrong.
    It probably would be better to ask this in a comp.lang.c++ since
    this is more in the territory of expertise of the regulars over
    there. See also



    I try a short explanation anyway (which might be wrong since I
    am not a C++ expert!): In C++ you can have several functons with
    the same name that are only distinguished by the number and
    types of their arguments. To allow the linker to distinuish be-
    tween these functions the compiler renames the functions auto-
    matically, typically by appending some text to the names that
    indicates the number and types of arguments, like

    foo( int ) becomes foo_i
    foo( double ) becomes foo_d

    (this is an example, not the real thing, different C++ compilers
    may do it in different ways). If you know this mangled name then
    you can change the call of the function accordingly, i.e. use the
    mangled name in the C code. The obvious drawback is that this
    only works for the C++ compiler you determined for how it does
    the name mangling.

    If you can change the C++ source and there's only a single
    function with that name then you can declare it in the C++
    header file with

    extern "C" void f(int i, char c, float x);

    to keep the C++ compiler from mangling the function name.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___ jt@toerring.de
    \______________ ____________ http://toerring.de

    Comment

    • CBFalconer

      #3
      Re: call c++ function from c

      falk.von.roetel @web.de wrote:
      >
      I have the problem to call a c++ funtion in a C project. I have a
      header file from the cpp file. In the header file the function is
      decleared "int GetHostIPAddres ses(ini i);". I inlcude the header
      file in the c file and call the function GetHostIPAddres ses(1);.
      But the linker doesn't found the function. Can someone give me a
      hint what is wrong.
      You can't do it. C++ has provisions for calling C functions, but C
      doesn't know anything about C++. Even the C++ calling is
      restricted to the same compiler.

      --
      Chuck F (cbfalconer at maineline dot net)
      Available for consulting/temporary embedded and systems.
      <http://cbfalconer.home .att.net>



      --
      Posted via a free Usenet account from http://www.teranews.com

      Comment

      • Malcolm McLean

        #4
        Re: call c++ function from c


        <falk.von.roete l@web.dewrote in message
        news:1186655858 .168109.172200@ k79g2000hse.goo glegroups.com.. .
        Hello,
        >
        I have the problem to call a c++ funtion in a C project. I have a
        header file from the cpp file. In the header file the function is
        decleared "int GetHostIPAddres ses(ini i);". I inlcude the header file
        in the c file and call the function GetHostIPAddres ses(1);. But the
        linker doesn't found the function. Can someone give me a hint what is
        wrong.
        >
        Get the C++ object file and dump it as ascii. You should see all the
        functions arrayed, but because it is C++ they have funny characters and
        other things associated with them.
        Extract the name - which may take two or three attempts - put it in your C
        source, and try to link. Eventually you will succeed. Then test the function
        carefully to make sure C++ isn't passing it any extra arguments.

        --
        Free games and programming goodies.


        Comment

        • Keith Thompson

          #5
          Re: call c++ function from c

          "Malcolm McLean" <regniztar@btin ternet.comwrite s:
          <falk.von.roete l@web.dewrote in message
          news:1186655858 .168109.172200@ k79g2000hse.goo glegroups.com.. .
          >I have the problem to call a c++ funtion in a C project. I have a
          >header file from the cpp file. In the header file the function is
          >decleared "int GetHostIPAddres ses(ini i);". I inlcude the header file
          >in the c file and call the function GetHostIPAddres ses(1);. But the
          >linker doesn't found the function. Can someone give me a hint what is
          >wrong.
          >>
          Get the C++ object file and dump it as ascii. You should see all the
          functions arrayed, but because it is C++ they have funny characters
          and other things associated with them.
          Extract the name - which may take two or three attempts - put it in
          your C source, and try to link. Eventually you will succeed. Then test
          the function carefully to make sure C++ isn't passing it any extra
          arguments.
          In other words, proceed by trial and error to produce code that's
          going to be horribly non-portable because it depends intimately on how
          the particular C++ compiler performs "name mangling".

          A much better idea: read section 32 of the "C++ FAQ Lite" at
          <http://www.parashift.c om/c++-faq-lite/mixing-c-and-cpp.html>, and
          take any further questions to comp.lang.c++.

          --
          Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
          San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
          "We must do something. This is something. Therefore, we must do this."
          -- Antony Jay and Jonathan Lynn, "Yes Minister"

          Comment

          • Marcin Wolcendorf

            #6
            Re: call c++ function from c

            falk.von.roetel @web.de wrote:
            Hello,
            >
            I have the problem to call a c++ funtion in a C project. I have a
            header file from the cpp file. In the header file the function is
            decleared "int GetHostIPAddres ses(ini i);". I inlcude the header file
            in the c file and call the function GetHostIPAddres ses(1);. But the
            linker doesn't found the function. Can someone give me a hint what is
            wrong.
            Well, other explaind why it doesn't work pretty well, so I'll skip this
            part.
            If you want to use C++ function in C and you don't want to change C++
            code, add a wrapper layer. Decalre function in C++ that calls your
            intended function- it should look something like this:

            foo() - your C++ function.

            In .cpp file:
            extern "C" foo_for_C()
            { return foo()}

            In .h file:
            #ifdef __cplusplus
            extern "C" {
            #endif
            foo_for_c();

            #ifdef __cplusplus
            }
            #endif


            M.

            Comment

            • Bill Reid

              #7
              Re: call c++ function from c


              Marcin Wolcendorf <wolcendo@friko 2.onet.plwrote in message
              news:f9ho4k$lce $1@nemesis.news .tpi.pl...
              falk.von.roetel @web.de wrote:

              I have the problem to call a c++ funtion in a C project. I have a
              header file from the cpp file. In the header file the function is
              decleared "int GetHostIPAddres ses(ini i);". I inlcude the header file
              in the c file and call the function GetHostIPAddres ses(1);. But the
              linker doesn't found the function. Can someone give me a hint what is
              wrong.
              >
              Well, other explaind why it doesn't work pretty well, so I'll skip this
              part.
              If you want to use C++ function in C and you don't want to change C++
              code, add a wrapper layer. Decalre function in C++ that calls your
              intended function- it should look something like this:
              >
              foo() - your C++ function.
              >
              In .cpp file:
              extern "C" foo_for_C()
              { return foo()}
              >
              In .h file:
              #ifdef __cplusplus
              extern "C" {
              #endif
              foo_for_c();
              >
              #ifdef __cplusplus
              }
              #endif
              Well, looky there, an actual correct answer in this newsgroup. This
              question has come up several times here, and certain people have once
              again chosen to not only be presumably off-topic but just completely
              wrong as well. They were affirmatively corrected several times
              before, but apparently they are uneducable.

              And I'm quite sure, quite worthless as programmers or for any
              type of positive contribution to the human race. They are the garbage
              that lie to get a job, then dog it for as long as they can with a constant
              stream of excuses, lies, abuse, and obnoxious behavior in lieu of
              actually ever writing any type of useful software.

              In other words, the epitomy of how most employers are forced
              to view "software engineers" due to long painful experience...

              ---
              William Ernest Reid



              Comment

              • Chris Torek

                #8
                Re: call c++ function from c

                >falk.von.roetel @web.de wrote:
                >>>I have the problem to call a c++ funtion in a C project. ...
                >Marcin Wolcendorf <wolcendo@friko 2.onet.plwrote in message
                >news:f9ho4k$lc e$1@nemesis.new s.tpi.pl...
                >Well, other explaind why it doesn't work pretty well, so I'll skip this
                >part.
                >If you want to use C++ function in C and you don't want to change C++
                >code, add a wrapper [via C++'s extern "C" method]. [example snipped]
                In article <707vi.416238$p 47.254853@bgtns c04-news.ops.worldn et.att.net>,
                Bill Reid <hormelfree@hap pyhealthy.netwr ote:
                >Well, looky there, an actual correct answer in this newsgroup.
                Yes, except for one problem: the example code (that I snipped,
                above) is wrong. The bug is small and simple and easily corrected
                (and will be caught by the compiler), but still, the example contains
                a bug.

                (In fact, the C part of the example is valid C89 but not valid C99.
                This is, in effect, what is wrong with the C++ part of the example.)
                >This question has come up several times here ...
                And the answer here is "C++ defines a way to call C but not vice
                versa, so you will get a better (peer-reviewed, as it were) answer
                in comp.lang.c++, so you would be wiser to go there for advice."
                >... and certain people have once again chosen to not only be
                >presumably off-topic but just completely wrong as well.
                No, you and Marcin Wolcendorf were only *slightly* wrong. :-)

                This sort of subtle error is *why* one should seek answers in a
                more-appropriate forum. A newsgroup full of POSIX-specializing
                programmers (such as comp.unix.progr ammer) is more likely to know
                all the details of, say, using POSIX AIO -- even from C code --
                than is a forum of "strictly ANSI/ISO C programmers" like the
                comp.lang.c newsgroup.
                --
                In-Real-Life: Chris Torek, Wind River Systems
                Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
                email: forget about it http://web.torek.net/torek/index.html
                Reading email is like searching for food in the garbage, thanks to spammers.

                Comment

                • Marcin Wolcendorf

                  #9
                  Re: call c++ function from c

                  Chris Torek <nospam@torek.n etwrote:
                  falk.von.roetel @web.de wrote:
                  >>I have the problem to call a c++ funtion in a C project. ...
                  >
                  Marcin Wolcendorf <wolcendo@friko 2.onet.plwrote in message
                  news:f9ho4k$lce $1@nemesis.news .tpi.pl...
                  Well, other explaind why it doesn't work pretty well, so I'll skip this
                  part.
                  If you want to use C++ function in C and you don't want to change C++
                  code, add a wrapper [via C++'s extern "C" method]. [example snipped]
                  >
                  In article <707vi.416238$p 47.254853@bgtns c04-news.ops.worldn et.att.net>,
                  Bill Reid <hormelfree@hap pyhealthy.netwr ote:
                  Well, looky there, an actual correct answer in this newsgroup.
                  >
                  Yes, except for one problem: the example code (that I snipped,
                  above) is wrong. The bug is small and simple and easily corrected
                  (and will be caught by the compiler), but still, the example contains
                  a bug.
                  >
                  (In fact, the C part of the example is valid C89 but not valid C99.
                  This is, in effect, what is wrong with the C++ part of the example.)
                  I should have written 'foo(....)', so you wouldn't be confused. Well, I
                  assumed, possibly wrongly, that 'looks something like that' is an
                  example that might not compile, but shows a concept. Perahaps I should
                  have put it in the footnote...
                  >
                  This question has come up several times here ...
                  >
                  And the answer here is "C++ defines a way to call C but not vice
                  versa, so you will get a better (peer-reviewed, as it were) answer
                  in comp.lang.c++, so you would be wiser to go there for advice."
                  >
                  ... and certain people have once again chosen to not only be
                  presumably off-topic but just completely wrong as well.
                  >
                  No, you and Marcin Wolcendorf were only *slightly* wrong. :-)
                  >
                  This sort of subtle error is *why* one should seek answers in a
                  more-appropriate forum. A newsgroup full of POSIX-specializing
                  programmers (such as comp.unix.progr ammer) is more likely to know
                  all the details of, say, using POSIX AIO -- even from C code --
                  than is a forum of "strictly ANSI/ISO C programmers" like the
                  comp.lang.c newsgroup.
                  >
                  Have you actually read the description of this newsgroup? It's nice and
                  clear, one line only:
                  'Disscussion about C.'
                  I can't see here any 'strictly' nor 'ANSI' nor 'ISO'. The 'programmers'
                  have also been lost somehere in translation... The only match is 'C'.
                  Where *exactly* have you taken the description of this group from?


                  Regards,

                  M.W.

                  Comment

                  • CBFalconer

                    #10
                    Re: call c++ function from c

                    Marcin Wolcendorf wrote:
                    >
                    .... snip ...
                    >
                    Have you actually read the description of this newsgroup? It's
                    nice and clear, one line only:
                    'Disscussion about C.'
                    I can't see here any 'strictly' nor 'ANSI' nor 'ISO'. The
                    'programmers' have also been lost somehere in translation...
                    The only match is 'C'. Where *exactly* have you taken the
                    description of this group from?
                    The only factual descriptions of the C language are the ISO
                    standard(s) and, earlier, K & R.

                    --
                    Chuck F (cbfalconer at maineline dot net)
                    Available for consulting/temporary embedded and systems.
                    <http://cbfalconer.home .att.net>



                    --
                    Posted via a free Usenet account from http://www.teranews.com

                    Comment

                    • Sudhanshu

                      #11
                      Re: call c++ function from c

                      header.h file
                      #ifdef__cpluspl us
                      extern "C" {
                      #endif
                      int GetHostIPAddres ses(ini i);
                      #ifdef__cpluspl us
                      }
                      #endif

                      cpluplus.cpp
                      #include <header.h>
                      int GetHostIPAddres ses(ini i)
                      {
                      ......
                      }

                      cfile.c
                      #include <header.h>

                      Comment

                      • CBFalconer

                        #12
                        Re: call c++ function from c

                        Sudhanshu wrote:
                        >
                        header.h file
                        #ifdef__cpluspl us
                        extern "C" {
                        #endif
                        int GetHostIPAddres ses(ini i);
                        #ifdef__cpluspl us
                        }
                        #endif
                        >
                        cpluplus.cpp
                        #include <header.h>
                        int GetHostIPAddres ses(ini i)
                        {
                        .....
                        }
                        >
                        cfile.c
                        #include <header.h>
                        That enables C++ to call C, not the reverse direction.

                        Include adequate quotation from whatever you are answering, and
                        indent your code properly. Don't use tabs, use spaces.

                        --
                        Chuck F (cbfalconer at maineline dot net)
                        Available for consulting/temporary embedded and systems.
                        <http://cbfalconer.home .att.net>



                        --
                        Posted via a free Usenet account from http://www.teranews.com

                        Comment

                        • Chris Torek

                          #13
                          Re: call c++ function from c

                          In article <46C3BB0A.D2DD9 F8D@yahoo.com>,
                          CBFalconer <cbfalconer@mai neline.netwrote :
                          >[Using C++'s extern "C" construct] enables C++ to call C, not
                          >the reverse direction.
                          Actually, it works both ways, provided some reasonably obvious
                          and straightforward restrictions are met.

                          (It is possible to write C++ code that can*not* call C code, nor
                          vice versa; and mixing one vendor's C compiler with another's C++
                          compiler can result in various disasters, but this is equally true
                          for mixing one vendor's C compiler with another vendor's C compiler.
                          But if the calls can be done at all, the C++ extern "C" method is
                          almost always the way to go. Further discussion about this really
                          belongs in comp.lang.c++ and/or vendor-specific newsgroups, of
                          course.)
                          --
                          In-Real-Life: Chris Torek, Wind River Systems
                          Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
                          email: forget about it http://web.torek.net/torek/index.html
                          Reading email is like searching for food in the garbage, thanks to spammers.

                          Comment

                          • Chris Torek

                            #14
                            Re: call c++ function from c

                            Background: Marcin Wolcendorf provided a mostly-correct and suitably
                            worded answer, to which I did not object. Then:
                            >>In article <707vi.416238$p 47.254853@bgtns c04-news.ops.worldn et.att.net>
                            >>Bill Reid <hormelfree@hap pyhealthy.netwr ote:
                            >>>Well, looky there, an actual correct answer in this newsgroup.
                            >Chris Torek <nospam@torek.n etwrote:
                            >Yes, except for one problem: the example code (that I snipped,
                            >above) is wrong. The bug is small and simple and easily corrected
                            >(and will be caught by the compiler), but still, the example contains
                            >a bug.
                            >>
                            >(In fact, the C part of the example is valid C89 but not valid C99.
                            >This is, in effect, what is wrong with the C++ part of the example.)
                            In article <f9t46r$adm$1@n emesis.news.tpi .pl>,
                            Marcin Wolcendorf <wolcendo@friko 2.onet.plwrote:
                            >I should have written 'foo(....)', so you wouldn't be confused. Well, I
                            >assumed, possibly wrongly, that 'looks something like that' is an
                            >example that might not compile, but shows a concept.
                            Yes -- this was, I think, the main reason I did not object at all
                            to your own posting. I was really responding to Bill Reid:
                            >This sort of subtle error is *why* one should seek answers in a
                            >more-appropriate forum. A newsgroup full of POSIX-specializing
                            >programmers (such as comp.unix.progr ammer) is more likely to know
                            >all the details of, say, using POSIX AIO -- even from C code --
                            >than is a forum of "strictly ANSI/ISO C programmers" like the
                            >comp.lang.c newsgroup.
                            >Have you actually read the description of this newsgroup? It's nice and
                            >clear, one line only:
                            >'Disscussion about C.'
                            >I can't see here any 'strictly' nor 'ANSI' nor 'ISO'.
                            The lack of anything more specific -- the generality of the phrase
                            "discussion about C" -- is precisely what creates this constraint!
                            In a group that addresses generic "C" -- not "C on Tandem Non-Stop",
                            not "C on VMS", not "the C routines provided by vxWorks" -- what
                            sort of advice will you get on when to use semBCreate() versus
                            semMCreate()? Who will tell you, correctly, all the details about
                            SYS$QIO? Will you get correct information about thread programming
                            here, or are you more likely to get correct information elsewhere?

                            Perhaps you think we *should* discuss these here. But then I would
                            have to wonder why we have comp.os.vms and comp.programmin g.threads.

                            (Incidentally, one should use semBCreate if the mutex will be used
                            for interrupt code. One should use semMCreate if the mutex needs
                            to do automatic priority elevation to avoid inversions, or to set
                            various options. Otherwise either one will generally suffice.)
                            --
                            In-Real-Life: Chris Torek, Wind River Systems
                            Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
                            email: forget about it http://web.torek.net/torek/index.html
                            Reading email is like searching for food in the garbage, thanks to spammers.

                            Comment

                            • CBFalconer

                              #15
                              Re: call c++ function from c

                              Chris Torek wrote:
                              CBFalconer <cbfalconer@mai neline.netwrote :
                              >
                              >[Using C++'s extern "C" construct] enables C++ to call C, not
                              >the reverse direction.
                              >
                              Actually, it works both ways, provided some reasonably obvious
                              and straightforward restrictions are met.
                              >
                              (It is possible to write C++ code that can*not* call C code, nor
                              vice versa; and mixing one vendor's C compiler with another's C++
                              compiler can result in various disasters, but this is equally true
                              for mixing one vendor's C compiler with another vendor's C compiler.
                              But if the calls can be done at all, the C++ extern "C" method is
                              almost always the way to go. Further discussion about this really
                              belongs in comp.lang.c++ and/or vendor-specific newsgroups, of
                              course.)
                              The C++ system attempts to adorn external calls with information
                              about parameters, etc. in various ways. The C system has no way of
                              understanding this mess. Thus C can't call C++ bcause the
                              'adornments' are missing. However, C++ has a provision for
                              dropping those 'adornments' to call precompiled C code.

                              Since C provision for linking to C++ only involves a couple of one
                              line #includes, and no effort at the C++ level, I consider it
                              discussable here. C99 includes the reserved identifier
                              __cplusplus__ to guard those #includes.

                              --
                              Chuck F (cbfalconer at maineline dot net)
                              Available for consulting/temporary embedded and systems.
                              <http://cbfalconer.home .att.net>



                              --
                              Posted via a free Usenet account from http://www.teranews.com

                              Comment

                              Working...