how to call functions in C++ code from c

Discussion in 'C++' started by Mike Qin, Feb 13, 2004.

  1. Mike Qin

    Mike Qin Guest

    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
    Mike Qin, Feb 13, 2004
    #1
    1. Advertising

  2. Mike Qin

    Ron Natalie Guest

    "Mike Qin" <> wrote in message news:c0j84m$77k$...

    > 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.
    Ron Natalie, Feb 13, 2004
    #2
    1. Advertising

  3. "Ron Natalie" <> wrote in message
    news:402d25c6$0$161$...
    >
    > "Mike Qin" <> wrote in message

    news:c0j84m$77k$...
    >
    > > 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.


    Excuse my ignorance, what is a "non-trivial constructor in C?"
    Jeremy Cowles, Feb 13, 2004
    #3
  4. Mike Qin

    Ian Guest

    Ron Natalie wrote:
    > "Mike Qin" <> wrote in message news:c0j84m$77k$...
    >
    >
    >>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.
    >


    Or a trivial one, or any member functions.

    Ian
    Ian, Feb 13, 2004
    #4
  5. Mike Qin

    Ron Natalie Guest

    "Jeremy Cowles" <jeremy.cowles[nosp@m]asifl.com> wrote in message news:DAbXb.36621$...
    >
    > "Ron Natalie" <> wrote in message
    > news:402d25c6$0$161$...
    > >
    > > "Mike Qin" <> wrote in message

    > news:c0j84m$77k$...
    > >
    > > > 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.

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

    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.
    Ron Natalie, Feb 13, 2004
    #5
  6. Mike Qin

    Ron Natalie Guest

    "Ian" <> wrote in message news:...
    > Ron Natalie wrote:
    > > "Mike Qin" <> wrote in message news:c0j84m$77k$...
    > >
    > >
    > >>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.
    > >

    >
    > Or a trivial one, or any member functions.
    >

    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).
    Ron Natalie, Feb 13, 2004
    #6
  7. Mike Qin

    Evan Carew Guest

    -----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:
    > "Mike Qin" <> wrote in message news:c0j84m$77k$...
    >
    >
    >>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.
    >


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

    iD8DBQFALVTioo/Prlj9GScRAkFGAJ93AbDBo4cQUWx/wnBt/cEGy9aXmwCeN2JT
    hc8lutsPWguI8H+M6orjVaY=
    =qz1f
    -----END PGP SIGNATURE-----
    Evan Carew, Feb 13, 2004
    #7
  8. Mike Qin

    Ron Natalie Guest

    "Evan Carew" <> wrote in message news:102e ...
    > -----BEGIN PGP SIGNED MESSAGE-----

    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.
    Ron Natalie, Feb 13, 2004
    #8
  9. Mike Qin

    Evan Carew Guest

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

    Ron,

    I direct you to the ops original question:


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


    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:
    > "Evan Carew" <> wrote in message news:102e ...
    >
    >>-----BEGIN PGP SIGNED MESSAGE-----

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


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

    iD8DBQFALWaVoo/Prlj9GScRAkoTAJ94aDdBWZxsqlBCOplrQs4cmIm5QQCfTUJy
    3qvVjov8yyfmJv5HtxMSftk=
    =NN7b
    -----END PGP SIGNATURE-----
    Evan Carew, Feb 14, 2004
    #9
  10. Mike Qin

    Ian Guest

    Ron Natalie wrote:
    > "Ian" <> wrote in message news:...
    >
    >>Ron Natalie wrote:
    >>
    >>>"Mike Qin" <> wrote in message news:c0j84m$77k$...
    >>>
    >>>
    >>>
    >>>>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.
    >>>

    >>
    >>Or a trivial one, or any member functions.
    >>

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


    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
    Ian, Feb 14, 2004
    #10
  11. Mike Qin

    Ron Natalie Guest

    "Ian" <> wrote in message news:...
    > 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".


    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.
    Ron Natalie, Feb 17, 2004
    #11
  12. Mike Qin

    Ron Natalie Guest

    "Evan Carew" <> wrote in message news:...
    > 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.
    >

    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.
    Ron Natalie, Feb 17, 2004
    #12
  13. Mike Qin

    Mike Qin Guest

    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_entry(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:
    > -----BEGIN PGP SIGNED MESSAGE-----
    > Hash: SHA1
    >
    > Ron,
    >
    > I direct you to the ops original question:
    >
    >
    > >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.

    >
    > 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.
    >
    >
    Mike Qin, Feb 24, 2004
    #13
  14. Mike Qin

    giganews Guest

    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" <> wrote in message
    news:c1g3pf$e8s$...
    > 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_entry(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:
    > > -----BEGIN PGP SIGNED MESSAGE-----
    > > Hash: SHA1
    > >
    > > Ron,
    > >
    > > I direct you to the ops original question:
    > >
    > >
    > > >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.

    > >
    > > 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.
    > >
    > >
    giganews, Feb 26, 2004
    #14
  15. Mike Qin

    mohammadazim

    Joined:
    Nov 24, 2007
    Messages:
    2
    Thanks a lot


    Hi Evan I would like to thank you for providing sun url for C C++ mix code. I was just searching over the net for a similar problem I was facing and this thread did help me a lot.
    I would also like to see your examples.
    How can I get them offline?

    Thanks
    mohammadazim, Nov 26, 2007
    #15
  16. Mike Qin

    mohammadazim

    Joined:
    Nov 24, 2007
    Messages:
    2
    Hi Javier,

    You are right but I had a problem using some C functions in C++ code. I was writing C++ class for socket abstraction and was able to use allmost all calls like bind, listen, accept and connect. But problem came with read and write calls. In C these were compiling well. But in C++ compiler was giving "read not declared in this scope" error.
    So what I did is I wrote wrappers over read and write and compiled this code with C compiler. Then I used the header containing wrappers in my C++ code with extern "C" specifier. It compiled well. While linking I used c++ linker linked the C module also with other C++ modules and now it works fine.
    May be my explaination will help Mike too.
    Last edited: Nov 26, 2007
    mohammadazim, Nov 26, 2007
    #16
    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. CW
    Replies:
    3
    Views:
    6,140
    John Sivilla
    May 2, 2004
  2. tshad
    Replies:
    11
    Views:
    818
    tshad
    May 27, 2005
  3. Xiangliang Meng
    Replies:
    1
    Views:
    1,592
    Victor Bazarov
    Jun 21, 2004
  4. beginner
    Replies:
    7
    Views:
    549
    Alex Martelli
    Aug 19, 2007
  5. MisterE

    Read/copy/call a functions machine code?

    MisterE, Feb 18, 2008, in forum: C Programming
    Replies:
    25
    Views:
    820
    Kenneth Brody
    Feb 19, 2008
Loading...

Share This Page