how to call functions in C++ code from c

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

    how to call functions in C++ code from c

    Hi there,

    Now I'm writing code mainly in C++. And some existing c code
    will call some functions which are in a C++ class. I read the basic
    techniques introduced from a SUN's web site.
    Basically
    1. we can add extern "C" keyword, or
    2. use the object in C++ code as struct in C.


    My question is: do we have to initialize the object in c? because
    there is no constructor in c, will it call a default constructor in c++
    when we declare

    struct M;
    in c, M is an object defined in C++?

    Is there any detailed reference available on net?

    TIA

    Mike
  • Ron Natalie

    #2
    Re: how to call functions in C++ code from c


    "Mike Qin" <lqin@sce.carle ton.ca> wrote in message news:c0j84m$77k $1@driftwood.cc s.carleton.ca.. .
    [color=blue]
    > My question is: do we have to initialize the object in c? because
    > there is no constructor in c, will it call a default constructor in c++
    > when we declare
    >[/color]
    You can not use any class with a non-trivial constructor in C.

    Comment

    • Jeremy Cowles

      #3
      Re: how to call functions in C++ code from c


      "Ron Natalie" <ron@sensor.com > wrote in message
      news:402d25c6$0 $161$9a6e19ea@n ews.newshosting .com...[color=blue]
      >
      > "Mike Qin" <lqin@sce.carle ton.ca> wrote in message[/color]
      news:c0j84m$77k $1@driftwood.cc s.carleton.ca.. .[color=blue]
      >[color=green]
      > > My question is: do we have to initialize the object in c? because
      > > there is no constructor in c, will it call a default constructor in c++
      > > when we declare
      > >[/color]
      > You can not use any class with a non-trivial constructor in C.[/color]

      Excuse my ignorance, what is a "non-trivial constructor in C?"

      Comment

      • Ian

        #4
        Re: how to call functions in C++ code from c

        Ron Natalie wrote:[color=blue]
        > "Mike Qin" <lqin@sce.carle ton.ca> wrote in message news:c0j84m$77k $1@driftwood.cc s.carleton.ca.. .
        >
        >[color=green]
        >>My question is: do we have to initialize the object in c? because
        >>there is no constructor in c, will it call a default constructor in c++
        >>when we declare
        >>[/color]
        >
        > You can not use any class with a non-trivial constructor in C.
        >[/color]

        Or a trivial one, or any member functions.

        Ian

        Comment

        • Evan Carew

          #5
          Re: how to call functions in C++ code from c

          -----BEGIN PGP SIGNED MESSAGE-----
          Hash: SHA1

          <sigh/>

          Calling C++ objects from C, even with non-trivial constructors is
          trivial, once you know the trick. The deal is that you need to write a C
          wrapper for your C++ class library which handles the initialization of
          your classes. This wrapper needs to be compiled with extern "C" {} with
          your C++ compiler. your project can then be compiled against the wrapper
          (but not LINKED!). To link, you then run your C++ linker with both your
          C & C++ code, and voila, a runnable program .

          If you would like to be sent a working example project for the gcc tool
          chain, let me know off line & I can send it to you.

          Evan Carew

          Ron Natalie wrote:[color=blue]
          > "Mike Qin" <lqin@sce.carle ton.ca> wrote in message news:c0j84m$77k $1@driftwood.cc s.carleton.ca.. .
          >
          >[color=green]
          >>My question is: do we have to initialize the object in c? because
          >>there is no constructor in c, will it call a default constructor in c++
          >>when we declare
          >>[/color]
          >
          > You can not use any class with a non-trivial constructor in C.
          >[/color]

          -----BEGIN PGP SIGNATURE-----
          Version: GnuPG v1.0.6 (GNU/Linux)
          Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

          iD8DBQFALVTioo/Prlj9GScRAkFGAJ 93AbDBo4cQUWx/wnBt/cEGy9aXmwCeN2JT
          hc8lutsPWguI8H+ M6orjVaY=
          =qz1f
          -----END PGP SIGNATURE-----

          Comment

          • Ron Natalie

            #6
            Re: how to call functions in C++ code from c


            "Jeremy Cowles" <jeremy.cowle s[nosp@m]asifl.com> wrote in message news:DAbXb.3662 1$Bb.255756@twi ster.tampabay.r r.com...[color=blue]
            >
            > "Ron Natalie" <ron@sensor.com > wrote in message
            > news:402d25c6$0 $161$9a6e19ea@n ews.newshosting .com...[color=green]
            > >
            > > "Mike Qin" <lqin@sce.carle ton.ca> wrote in message[/color]
            > news:c0j84m$77k $1@driftwood.cc s.carleton.ca.. .[color=green]
            > >[color=darkred]
            > > > My question is: do we have to initialize the object in c? because
            > > > there is no constructor in c, will it call a default constructor in c++
            > > > when we declare
            > > >[/color]
            > > You can not use any class with a non-trivial constructor in C.[/color]
            >
            > Excuse my ignorance, what is a "non-trivial constructor in C?"
            >[/color]
            You can not use in C a class with a non-trivial constructor.

            A class has a non-trivial constructor if it has a user defined constructor or
            any of it's base classes or members have non-trivial constructors.

            Comment

            • Ron Natalie

              #7
              Re: how to call functions in C++ code from c


              "Ian" <noone@nowhere. com> wrote in message news:402D4BCC.7 000300@nowhere. com...[color=blue]
              > Ron Natalie wrote:[color=green]
              > > "Mike Qin" <lqin@sce.carle ton.ca> wrote in message news:c0j84m$77k $1@driftwood.cc s.carleton.ca.. .
              > >
              > >[color=darkred]
              > >>My question is: do we have to initialize the object in c? because
              > >>there is no constructor in c, will it call a default constructor in c++
              > >>when we declare
              > >>[/color]
              > >
              > > You can not use any class with a non-trivial constructor in C.
              > >[/color]
              >
              > Or a trivial one, or any member functions.
              >[/color]
              Sorry the above is wrong. You obviously don't know that "trivial constructor"
              is a precise term which does not mean "simple".

              Member functions (other than a few special ones like assignment operators, constructors,
              and destructors) do not affect usability in C (other than the fact that C obviously can't call
              them directly).

              Comment

              • Ron Natalie

                #8
                Re: how to call functions in C++ code from c


                "Evan Carew" <tempcarew@pobo x.com> wrote in message news:102e qkrmhdbm46bf@co rp.supernews.co m...[color=blue]
                > -----BEGIN PGP SIGNED MESSAGE-----[/color]
                Thanks fo the gratuitous and ignorant answer. I am quite aware you can
                wrap C++ functiosn in extern C functions. That wasn't the question the
                user asked (nor the one I answered).

                I have no desire to get your lousy GCC programs via mail or otherwise.

                Comment

                • Evan Carew

                  #9
                  Re: how to call functions in C++ code from c

                  -----BEGIN PGP SIGNED MESSAGE-----
                  Hash: SHA1

                  Ron,

                  I direct you to the ops original question:

                  [color=blue]
                  >Hi there,
                  >
                  >Now I'm writing code mainly in C++. And some existing c code
                  >will call some functions which are in a C++ class. I read the basic
                  >techniques introduced from a SUN's web site.
                  >Basically
                  >1. we can add extern "C" keyword, or
                  >2. use the object in C++ code as struct in C.[/color]

                  In Mike's case, you do indeed need to be able to initialize a class
                  prior to utilize its "functions" unless they are static... even then,
                  I'm sure someone will come up with a case where a static needs
                  initialization. And, while he didn't expressly state his question in
                  these terms (likely because he didn't know what questions to ask) The
                  following issues are implied:

                  1. How precisely do you use extern "C" {} in such a cross language project?

                  2. How would you use C++ objects or types not found in C from C?

                  3. Once you have solved the previous two issues, how do you link the
                  resulting object code (tool chain specific unfortunately).

                  I can see that #3 from above will be of particular interest to Mike as
                  he says "I read the basic techniques introduced from a SUN's web site."
                  Having plied this particular pathway myself not too long ago, I can say
                  that info for this technique is sadly in short supply. I myself had to
                  write to the authors of C/C++ Users Journal to get all the issues in
                  hand after an article appeared in one of their previous issues which
                  touched on the subject. For this reason, when helping others get through
                  this subject, I provide a working project which gives the developer in
                  question a good idea of the plumbing in question.

                  I can also say that due to this simple project, I have received several
                  thank you emails off group. I have to say, that's a good feeling.

                  Please note, as with most news groups, there is a minimum expectation of
                  decorum which I, speaking for the rest of us, would hope you could see
                  your way to adhering to. Otherwise, well, I expect others like myself
                  will simply put your address in our kill files.


                  Ron Natalie wrote:[color=blue]
                  > "Evan Carew" <tempcarew@pobo x.com> wrote in message news:102e qkrmhdbm46bf@co rp.supernews.co m...
                  >[color=green]
                  >>-----BEGIN PGP SIGNED MESSAGE-----[/color]
                  >
                  > Thanks fo the gratuitous and ignorant answer. I am quite aware you can
                  > wrap C++ functiosn in extern C functions. That wasn't the question the
                  > user asked (nor the one I answered).
                  >
                  > I have no desire to get your lousy GCC programs via mail or otherwise.
                  >[/color]

                  -----BEGIN PGP SIGNATURE-----
                  Version: GnuPG v1.0.6 (GNU/Linux)
                  Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

                  iD8DBQFALWaVoo/Prlj9GScRAkoTAJ 94aDdBWZxsqlBCO plrQs4cmIm5QQCf TUJy
                  3qvVjov8yyfmJv5 HtxMSftk=
                  =NN7b
                  -----END PGP SIGNATURE-----

                  Comment

                  • Ian

                    #10
                    Re: how to call functions in C++ code from c

                    Ron Natalie wrote:[color=blue]
                    > "Ian" <noone@nowhere. com> wrote in message news:402D4BCC.7 000300@nowhere. com...
                    >[color=green]
                    >>Ron Natalie wrote:
                    >>[color=darkred]
                    >>>"Mike Qin" <lqin@sce.carle ton.ca> wrote in message news:c0j84m$77k $1@driftwood.cc s.carleton.ca.. .
                    >>>
                    >>>
                    >>>
                    >>>>My question is: do we have to initialize the object in c? because
                    >>>>there is no constructor in c, will it call a default constructor in c++
                    >>>>when we declare
                    >>>>
                    >>>
                    >>>You can not use any class with a non-trivial constructor in C.
                    >>>[/color]
                    >>
                    >>Or a trivial one, or any member functions.
                    >>[/color]
                    >
                    > Sorry the above is wrong. You obviously don't know that "trivial constructor"
                    > is a precise term which does not mean "simple".
                    >
                    > Member functions (other than a few special ones like assignment operators, constructors,
                    > and destructors) do not affect usability in C (other than the fact that C obviously can't call
                    > them directly).
                    >[/color]

                    Ok, so my understanding was incorrect. The two terms are often confused.

                    But the rest does apply. Show me a C compiler that will accept 'class'
                    or a struct with a member function. Assuming that "usable" means
                    "compilable ".

                    Example:

                    struct A;

                    typedef void (*fnp)(struct A*);

                    struct A
                    {
                    int n;

                    fnp fred;
                    };

                    Is valid C, while

                    typedef void (*fnp)(void);

                    struct A
                    {
                    int n;

                    void fred();
                    };

                    Is not.

                    Ian

                    Comment

                    • Ron Natalie

                      #11
                      Re: how to call functions in C++ code from c


                      "Ian" <noone@nowhere. com> wrote in message news:402D91A0.7 080103@nowhere. com...[color=blue]
                      > But the rest does apply. Show me a C compiler that will accept 'class'
                      > or a struct with a member function. Assuming that "usable" means
                      > "compilable ".[/color]

                      All you have to do is make the member functions conditional on __cplusplus.
                      Once you fix the syntax to be palatable to C, there is no restriction on their
                      presence on the C++ side. Member functions in general do not poison the classes
                      compatibility with C. It's only certain member functions (constructors, etc...)
                      that do.

                      Comment

                      • Ron Natalie

                        #12
                        Re: how to call functions in C++ code from c


                        "Evan Carew" <tempcarew@pobo x.com> wrote in message news:102qp98leo rsh92@corp.supe rnews.com...[color=blue]
                        > Please note, as with most news groups, there is a minimum expectation of
                        > decorum which I, speaking for the rest of us, would hope you could see
                        > your way to adhering to. Otherwise, well, I expect others like myself
                        > will simply put your address in our kill files.
                        >[/color]
                        You were the one who started your response off with an insult. My failing
                        is that I could not rise above your level of immaturity. I suggest you leave
                        them out of your future postings if you can't deal with being answered in kind.


                        Comment

                        • Mike Qin

                          #13
                          Re: how to call functions in C++ code from c

                          thanks everyone! I've already spent several days trying to
                          solve the problem, but still cannot.

                          here is my problem, one part code is already in C, named A,
                          other part code, named B, is in C++, I write a foo class in c++, taking
                          a massage from A and sending it to B.

                          in foo's herader file, I declare some wrapper functions,
                          also, I define a static pointer of Foo,

                          extern "C"{
                          int add_entry(entry anEntry);
                          void initializer()
                          ....
                          }
                          class Foo{
                          public:
                          //constructor
                          Foo(...);
                          int original_add_en try(entry one);
                          .....
                          }

                          Q: I cannot put the extern block inside the class Foo declaration

                          Also, I cannot
                          #include foo.h
                          in the A which in in C, otherwise there are more errors while compiling.
                          So I just declare "extern int add_entry(...)" in A.

                          Compilation is OK now, but I got link error when add_entry(...) is
                          called from A, (otherwise is OK).

                          "underfined reference to "some functions in the c++
                          collect2: ld returned 1 exist status"

                          I checked the web, it seems I didn't handle "extern" keyword properly.

                          Could anyone please give me some suggestion?

                          BTW, I initialized the static pointer.

                          TIA

                          Mike


                          Evan Carew wrote:[color=blue]
                          > -----BEGIN PGP SIGNED MESSAGE-----
                          > Hash: SHA1
                          >
                          > Ron,
                          >
                          > I direct you to the ops original question:
                          >
                          >[color=green]
                          > >Hi there,
                          > >
                          > >Now I'm writing code mainly in C++. And some existing c code
                          > >will call some functions which are in a C++ class. I read the basic
                          > >techniques introduced from a SUN's web site.
                          > >Basically
                          > >1. we can add extern "C" keyword, or
                          > >2. use the object in C++ code as struct in C.[/color]
                          >
                          > In Mike's case, you do indeed need to be able to initialize a class
                          > prior to utilize its "functions" unless they are static... even then,
                          > I'm sure someone will come up with a case where a static needs
                          > initialization. And, while he didn't expressly state his question in
                          > these terms (likely because he didn't know what questions to ask) The
                          > following issues are implied:
                          >
                          > 1. How precisely do you use extern "C" {} in such a cross language project?
                          >
                          > 2. How would you use C++ objects or types not found in C from C?
                          >
                          > 3. Once you have solved the previous two issues, how do you link the
                          > resulting object code (tool chain specific unfortunately).
                          >
                          > I can see that #3 from above will be of particular interest to Mike as
                          > he says "I read the basic techniques introduced from a SUN's web site."
                          > Having plied this particular pathway myself not too long ago, I can say
                          > that info for this technique is sadly in short supply. I myself had to
                          > write to the authors of C/C++ Users Journal to get all the issues in
                          > hand after an article appeared in one of their previous issues which
                          > touched on the subject. For this reason, when helping others get through
                          > this subject, I provide a working project which gives the developer in
                          > question a good idea of the plumbing in question.
                          >
                          > I can also say that due to this simple project, I have received several
                          > thank you emails off group. I have to say, that's a good feeling.
                          >
                          > Please note, as with most news groups, there is a minimum expectation of
                          > decorum which I, speaking for the rest of us, would hope you could see
                          > your way to adhering to. Otherwise, well, I expect others like myself
                          > will simply put your address in our kill files.
                          >
                          >[/color]

                          Comment

                          • giganews

                            #14
                            Re: how to call functions in C++ code from c

                            The extern statement tells the compiler not to worry about
                            the compilation (object) for the function declared. So your compilation
                            will succed but when at linking time...where is the object for
                            the extern??

                            If you are using a c compiler you may link a program that was
                            already COMPILED with a c++ compiler (g++).

                            What I'm saying is that you can't make uncompiled c++ code
                            using a c compiler part of your project using a c compiler,
                            you need to use a c++ compiler.

                            If you are using a c++ compiler then you can call any c function
                            anywhere without the need of externs.

                            Hope this helps.
                            Javier.

                            "Mike Qin" <lqin@sce.carle ton.ca> wrote in message
                            news:c1g3pf$e8s $1@driftwood.cc s.carleton.ca.. .[color=blue]
                            > thanks everyone! I've already spent several days trying to
                            > solve the problem, but still cannot.
                            >
                            > here is my problem, one part code is already in C, named A,
                            > other part code, named B, is in C++, I write a foo class in c++, taking
                            > a massage from A and sending it to B.
                            >
                            > in foo's herader file, I declare some wrapper functions,
                            > also, I define a static pointer of Foo,
                            >
                            > extern "C"{
                            > int add_entry(entry anEntry);
                            > void initializer()
                            > ...
                            > }
                            > class Foo{
                            > public:
                            > //constructor
                            > Foo(...);
                            > int original_add_en try(entry one);
                            > ....
                            > }
                            >
                            > Q: I cannot put the extern block inside the class Foo declaration
                            >
                            > Also, I cannot
                            > #include foo.h
                            > in the A which in in C, otherwise there are more errors while compiling.
                            > So I just declare "extern int add_entry(...)" in A.
                            >
                            > Compilation is OK now, but I got link error when add_entry(...) is
                            > called from A, (otherwise is OK).
                            >
                            > "underfined reference to "some functions in the c++
                            > collect2: ld returned 1 exist status"
                            >
                            > I checked the web, it seems I didn't handle "extern" keyword properly.
                            >
                            > Could anyone please give me some suggestion?
                            >
                            > BTW, I initialized the static pointer.
                            >
                            > TIA
                            >
                            > Mike
                            >
                            >
                            > Evan Carew wrote:[color=green]
                            > > -----BEGIN PGP SIGNED MESSAGE-----
                            > > Hash: SHA1
                            > >
                            > > Ron,
                            > >
                            > > I direct you to the ops original question:
                            > >
                            > >[color=darkred]
                            > > >Hi there,
                            > > >
                            > > >Now I'm writing code mainly in C++. And some existing c code
                            > > >will call some functions which are in a C++ class. I read the basic
                            > > >techniques introduced from a SUN's web site.
                            > > >Basically
                            > > >1. we can add extern "C" keyword, or
                            > > >2. use the object in C++ code as struct in C.[/color]
                            > >
                            > > In Mike's case, you do indeed need to be able to initialize a class
                            > > prior to utilize its "functions" unless they are static... even then,
                            > > I'm sure someone will come up with a case where a static needs
                            > > initialization. And, while he didn't expressly state his question in
                            > > these terms (likely because he didn't know what questions to ask) The
                            > > following issues are implied:
                            > >
                            > > 1. How precisely do you use extern "C" {} in such a cross language[/color][/color]
                            project?[color=blue][color=green]
                            > >
                            > > 2. How would you use C++ objects or types not found in C from C?
                            > >
                            > > 3. Once you have solved the previous two issues, how do you link the
                            > > resulting object code (tool chain specific unfortunately).
                            > >
                            > > I can see that #3 from above will be of particular interest to Mike as
                            > > he says "I read the basic techniques introduced from a SUN's web site."
                            > > Having plied this particular pathway myself not too long ago, I can say
                            > > that info for this technique is sadly in short supply. I myself had to
                            > > write to the authors of C/C++ Users Journal to get all the issues in
                            > > hand after an article appeared in one of their previous issues which
                            > > touched on the subject. For this reason, when helping others get through
                            > > this subject, I provide a working project which gives the developer in
                            > > question a good idea of the plumbing in question.
                            > >
                            > > I can also say that due to this simple project, I have received several
                            > > thank you emails off group. I have to say, that's a good feeling.
                            > >
                            > > Please note, as with most news groups, there is a minimum expectation of
                            > > decorum which I, speaking for the rest of us, would hope you could see
                            > > your way to adhering to. Otherwise, well, I expect others like myself
                            > > will simply put your address in our kill files.
                            > >
                            > >[/color][/color]


                            Comment

                            Working...