Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > how to call functions in C++ code from c

Reply
Thread Tools

how to call functions in C++ code from c

 
 
Mike Qin
Guest
Posts: n/a
 
      02-13-2004
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
 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      02-13-2004

"Mike Qin" <(E-Mail Removed)> wrote in message news:c0j84m$77k$(E-Mail Removed)...

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

 
Reply With Quote
 
 
 
 
Jeremy Cowles
Guest
Posts: n/a
 
      02-13-2004

"Ron Natalie" <(E-Mail Removed)> wrote in message
news:402d25c6$0$161$(E-Mail Removed). ..
>
> "Mike Qin" <(E-Mail Removed)> wrote in message

news:c0j84m$77k$(E-Mail Removed)...
>
> > 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?"

 
Reply With Quote
 
Ian
Guest
Posts: n/a
 
      02-13-2004
Ron Natalie wrote:
> "Mike Qin" <(E-Mail Removed)> wrote in message news:c0j84m$77k$(E-Mail Removed)...
>
>
>>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

 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      02-13-2004

"Jeremy Cowles" <jeremy.cowles[nosp@m]asifl.com> wrote in message newsAbXb.36621$(E-Mail Removed) ...
>
> "Ron Natalie" <(E-Mail Removed)> wrote in message
> news:402d25c6$0$161$(E-Mail Removed). ..
> >
> > "Mike Qin" <(E-Mail Removed)> wrote in message

> news:c0j84m$77k$(E-Mail Removed)...
> >
> > > 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.

 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      02-13-2004

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

 
Reply With Quote
 
Evan Carew
Guest
Posts: n/a
 
      02-13-2004
-----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" <(E-Mail Removed)> wrote in message news:c0j84m$77k$(E-Mail Removed)...
>
>
>>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-----
 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      02-13-2004

"Evan Carew" <(E-Mail Removed)> wrote in message news:102e http://www.velocityreviews.com/forums/(E-Mail Removed)...
> -----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.

 
Reply With Quote
 
Evan Carew
Guest
Posts: n/a
 
      02-14-2004
-----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" <(E-Mail Removed)> wrote in message news:102e (E-Mail Removed)...
>
>>-----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-----
 
Reply With Quote
 
Ian
Guest
Posts: n/a
 
      02-14-2004
Ron Natalie wrote:
> "Ian" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
>
>>Ron Natalie wrote:
>>
>>>"Mike Qin" <(E-Mail Removed)> wrote in message news:c0j84m$77k$(E-Mail Removed)...
>>>
>>>
>>>
>>>>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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to call module functions inside class instance functions? beginner Python 7 08-19-2007 05:48 AM
ASP.NET 2.0 - Dynamically create ActiveX object using HtmlGenericControl and call methods / functions all from code behind qualitynice@hotmail.com ASP .Net 4 11-04-2005 04:47 PM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM
How to call code in aspx webform from functions defined in code behind modules CW ASP .Net 3 05-02-2004 02:20 PM



Advertisments