473,320 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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
Jul 22 '05 #1
13 1549

"Mike Qin" <lq**@sce.carleton.ca> wrote in message news:c0**********@driftwood.ccs.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.

Jul 22 '05 #2

"Ron Natalie" <ro*@sensor.com> wrote in message
news:40*********************@news.newshosting.com. ..

"Mike Qin" <lq**@sce.carleton.ca> wrote in message news:c0**********@driftwood.ccs.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.


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

Jul 22 '05 #3
Ian
Ron Natalie wrote:
"Mike Qin" <lq**@sce.carleton.ca> wrote in message news:c0**********@driftwood.ccs.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.


Or a trivial one, or any member functions.

Ian

Jul 22 '05 #4
-----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" <lq**@sce.carleton.ca> wrote in message news:c0**********@driftwood.ccs.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.


-----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-----
Jul 22 '05 #5

"Jeremy Cowles" <jeremy.cowles[nosp@m]asifl.com> wrote in message news:DA*******************@twister.tampabay.rr.com ...

"Ron Natalie" <ro*@sensor.com> wrote in message
news:40*********************@news.newshosting.com. ..

"Mike Qin" <lq**@sce.carleton.ca> wrote in message

news:c0**********@driftwood.ccs.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.


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.

Jul 22 '05 #6

"Ian" <no***@nowhere.com> wrote in message news:40**************@nowhere.com...
Ron Natalie wrote:
"Mike Qin" <lq**@sce.carleton.ca> wrote in message news:c0**********@driftwood.ccs.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.


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

Jul 22 '05 #7

"Evan Carew" <te*******@pobox.com> wrote in message news:102e qk**********@corp.supernews.com...
-----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.

Jul 22 '05 #8
-----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" <te*******@pobox.com> wrote in message news:102e qk**********@corp.supernews.com...
-----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-----
Jul 22 '05 #9
Ian
Ron Natalie wrote:
"Ian" <no***@nowhere.com> wrote in message news:40**************@nowhere.com...
Ron Natalie wrote:
"Mike Qin" <lq**@sce.carleton.ca> wrote in message news:c0**********@driftwood.ccs.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.


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

Jul 22 '05 #10

"Ian" <no***@nowhere.com> wrote in message news:40**************@nowhere.com...
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.

Jul 22 '05 #11

"Evan Carew" <te*******@pobox.com> wrote in message news:10*************@corp.supernews.com...
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.
Jul 22 '05 #12
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.

Jul 22 '05 #13
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" <lq**@sce.carleton.ca> wrote in message
news:c1**********@driftwood.ccs.carleton.ca...
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.

Jul 22 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

39
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. ...
7
by: Tim ffitch | last post by:
Hi I have created a VB dll file that contains common functions I use across various projects in VB, Access and Excel. Rather than have to code the functions in each I decided to use the dll...
6
by: Mark Jerde | last post by:
I need to call a Win32 DLL. The API supports both Windows and Linux, but I'm only concerned with Windows. I'd like advice whether I can use C# or whether I'll have to dust off my old C++ books. ...
13
by: Bern McCarty | last post by:
I have run an experiment to try to learn some things about floating point performance in managed C++. I am using Visual Studio 2003. I was hoping to get a feel for whether or not it would make...
10
by: bienwell | last post by:
Hi, I have a question about file included in ASP.NET. I have a file that includes all the Sub functions (e.g FileFunct.vb). One of the functions in this file is : Sub TestFunct(ByVal...
11
by: Felix Kater | last post by:
Hi, I can compile and run this code (see below) which twice calls the function f, first with too less, second with too much arguments. But is it legal and free of memory leaks and other...
7
by: beginner | last post by:
Hi Everyone, I have encountered a small problems. How to call module functions inside class instance functions? For example, calling func1 in func2 resulted in a compiling error. "my module...
5
by: GCRhoads | last post by:
I have some templated functions and I want to write a call wrapper class for it. I also want to pass the function object from this class to some functions that will then call the function...
12
by: aaragon | last post by:
I have this scenario: several arrays for which I have their fixed values at compilation time. Now, at runtime I need to access a specific array depending on an integer but I want to avoid if and...
6
by: RandomElle | last post by:
Hi there I'm hoping someone can help me out with the use of the Eval function. I am using Access2003 under WinXP Pro. I can successfully use the Eval function and get it to call any function with...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.