how to call functions in C++ code from c

M

Mike Qin

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
 
R

Ron Natalie

Mike Qin said:
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.
 
R

Ron Natalie

Jeremy Cowles said:
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.
 
R

Ron Natalie

Ian said:
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).
 
E

Evan Carew

-----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 said:
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-----
 
R

Ron Natalie

Evan Carew said:
-----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.
 
E

Evan Carew

-----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 said:
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-----
 
I

Ian

Ron said:
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
 
R

Ron Natalie

Ian said:
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.
 
R

Ron Natalie

Evan Carew said:
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.
 
M

Mike Qin

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
 
G

giganews

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.
 
Joined
Nov 24, 2007
Messages
2
Reaction score
0
Thanks a lot

Evan Carew said:
-----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" <[email protected]> wrote in message news:102e (e-mail address 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-----


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
 
Joined
Nov 24, 2007
Messages
2
Reaction score
0
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:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top