472,353 Members | 1,178 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

C++ callback function passed to a C program

Hi all,

is there any trouble setting an C++ static class method as callback
function for a C program or library ?

Thanks
Jul 19 '05 #1
6 5171
Eric Entressangle wrote:
Hi all,

is there any trouble setting an C++ static class method as callback
function for a C program or library ?


Yes. The function will have C++ calling conventions, but be called using
C calling conventions. An some (many?) compilers, they are both the
same, but if they differ, it won't work. For maximum portability, you
should only use extern "C" functions for that, which is of course not
possible for member functions, even if static.

Jul 19 '05 #2
On Fri, 25 Jul 2003 18:22:21 +0200, Rolf Magnus <ra******@t-online.de> wrote:
Eric Entressangle wrote:
Hi all,

is there any trouble setting an C++ static class method as callback
function for a C program or library ?


Yes. The function will have C++ calling conventions, but be called using
C calling conventions. An some (many?) compilers, they are both the
same, but if they differ, it won't work. For maximum portability, you
should only use extern "C" functions for that, which is of course not
possible for member functions, even if static.


Using an 'extern "C"' function doesn't buy additional portability... ;-)

Reason: neither C nor C++ defines the machine-level calling convention,
nor name-mangling or other relevant things. With at least one of the
most used C and C++ compilers 'extern "C"' has _no_ effect on the
calling convention whatsoever. What it does influence with that
compiler is the name mangling, i.e. linkage compatibility: the compiler
trusts you to know what you're doing, since this spec removes type
information (e.g. name mangling) in order to be linkage-compatible
with C.

The main assumption for the callback problem is calling convention
compatibility, and linkage compatibility doesn't enter the picture, but
the details of the calling convention compatibility depends on the
compilers, and one should not assume that 'extern "C"' will influence
calling conventions, data type compatibility, or any such thing.

Jul 19 '05 #3
On Fri, 25 Jul 2003 19:54:33 +0200, Rolf Magnus <ra******@t-online.de> wrote:

Reason: neither C nor C++ defines the machine-level calling
convention, nor name-mangling or other relevant things.
I'm aware of that.


Then you should draw the logical conclusions from that,

[Silly and longish irrelevant quoting of the standard elided]From the standard's POV,
POV?

those things [calling convention, data types etc.] are all part
of the linkage.
Even different versions of the same C++ compiler can produce
incompatible object code, in full agreement with the standard.
So in other words, what you write is not correct.
Anyway, what would you gain if your compiler use the correct naming for
the functions for calling them from C, if the data types aren't
compatible or the parameters are passed differently?


You'd have to ask Kernighan and Ritchie about that... For in C you can
have (or at least could have) incompatibility between calling code
and called code even within the same compilation unit; hence the tendency
of C programmers to write "int main(void)", because simply leaving out the
argument list for a function would match any signature. When C++ is
interfaced to C there is no way the C++ standard can compensate, and so
it does not even try.

On a more practical level it's _your_ job to make sure that everyhing
matches between call and called function.

Achieving linking is just one of many considerations, and furthermore,
is not relevant to using a C++ function as a callback.

Jul 19 '05 #4
Alf P. Steinbach wrote:
On Fri, 25 Jul 2003 19:54:33 +0200, Rolf Magnus <ra******@t-online.de>
wrote:

Reason: neither C nor C++ defines the machine-level calling
convention, nor name-mangling or other relevant things.
I'm aware of that.


Then you should draw the logical conclusions from that,

[Silly and longish irrelevant quoting of the standard elided]
From the standard's POV,


POV?


Point Of View

those things [calling convention, data types etc.] are all part
of the linkage.
Even different versions of the same C++ compiler can produce
incompatible object code, in full agreement with the standard.
So in other words, what you write is not correct.


But the part of the standard I quoted just says that.
Anyway, what would you gain if your compiler use the correct naming
for the functions for calling them from C, if the data types aren't
compatible or the parameters are passed differently?


You'd have to ask Kernighan and Ritchie about that... For in C you
can have (or at least could have) incompatibility between calling code
and called code even within the same compilation unit; hence the
tendency of C programmers to write "int main(void)", because simply
leaving out the argument list for a function would match any
signature.


AFAIK, this is due to historical reasons. In C99, it's not true anymore.
When C++ is interfaced to C there is no way the C++ standard can
compensate, and so it does not even try.
Well, it doesn't say how the linkage looks like, but it says that the
compiler _must_ offer C linkage. Most C++ compilers also understand C
or are bundled with a C compiler, and I would be quite surprised if
they wouldn't just make their C linkage compatible with the C compiler.
On a more practical level it's _your_ job to make sure that everyhing
matches between call and called function.
Well, it's "my" job to make sure that the compilers agree about their C
linkage style.
Achieving linking is just one of many considerations, and furthermore,
is not relevant to using a C++ function as a callback.


Look again at the OP's question. He asked about static members as
callback for a C program or library. And so it definitely is relevant.
That was my point anyway. I just answered that making the callback
function extern "C" is the most portable solution. Do you disagree
about that?
Jul 19 '05 #5
On Fri, 25 Jul 2003 21:59:28 +0200, Rolf Magnus <ra******@t-online.de> wrote:
Alf P. Steinbach wrote:
On Fri, 25 Jul 2003 19:54:33 +0200, Rolf Magnus <ra******@t-online.de>
wrote:

Reason: neither C nor C++ defines the machine-level calling
convention, nor name-mangling or other relevant things.

I'm aware of that.


Then you should draw the logical conclusions from that,

[Silly and longish irrelevant quoting of the standard elided]
From the standard's POV,


POV?


Point Of View

those things [calling convention, data types etc.] are all part
of the linkage.


Even different versions of the same C++ compiler can produce
incompatible object code, in full agreement with the standard.
So in other words, what you write is not correct.


But the part of the standard I quoted just says that.


???

Anyway, what would you gain if your compiler use the correct naming
for the functions for calling them from C, if the data types aren't
compatible or the parameters are passed differently?


You'd have to ask Kernighan and Ritchie about that... For in C you
can have (or at least could have) incompatibility between calling code
and called code even within the same compilation unit; hence the
tendency of C programmers to write "int main(void)", because simply
leaving out the argument list for a function would match any
signature.


AFAIK, this is due to historical reasons. In C99, it's not true anymore.


Keep in mind that the C++ standard stems from 1997, and that 1997 < 1999.
When C++ is interfaced to C there is no way the C++ standard can
compensate, and so it does not even try.


Well, it doesn't say how the linkage looks like, but it says that the
compiler _must_ offer C linkage. Most C++ compilers also understand C
or are bundled with a C compiler, and I would be quite surprised if
they wouldn't just make their C linkage compatible with the C compiler.


As a concrete example, Visual C++ supports a number of different
calling conventions such as "__stdcall" (which oldtimers like me think
of as Pascal) and "__cdecl" (which we think of as "C", purely because
of typical usage). If you decorate a function with 'extern "C"',
then (with this widely used compiler) that does _not_ affect the
calling convention. It affects only the external name the linker sees
(wrt. to the standard there's not necessarily a linker involved, but
this scheme is, as far as I know, completely standard-conforming.)

The crux is that there's no such thing as "the" C compiler.

There are a number of different C compilers, and even using just one
compiler you can compile functions that look the same to the linker
(and so, no amount of fiddling in C++ can guarantee compatibility) but
which have different calling conventions, different sizes of int, or
whatever.

On a more practical level it's _your_ job to make sure that everyhing
matches between call and called function.


Well, it's "my" job to make sure that the compilers agree about their C
linkage style.


Yep.

Achieving linking is just one of many considerations, and furthermore,
is not relevant to using a C++ function as a callback.


Look again at the OP's question. He asked about static members as
callback for a C program or library. And so it definitely is relevant.
That was my point anyway. I just answered that making the callback
function extern "C" is the most portable solution. Do you disagree
about that?


Yep.

Jul 19 '05 #6
Alf P. Steinbach wrote:
But the part of the standard I quoted just says that.
???


I can repeat that quote again, though I don't know why you don't just
read it in my previous posting. So again, I said:
those things [calling convention, data types etc.] are all part
of the linkage.
And you said:
Even different versions of the same C++ compiler can produce
incompatible object code, in full agreement with the standard.
to which I agree, but not to:
So in other words, what you write is not correct.
I quoted the following sentence from the standard:

All function types, function names and variable names have a language
linkage. [Note: Some of the properties associated with an entity with
language linkage may be associated with a particular form of
representing names of objects and functions with external linkage, or
with a particular calling convention, etc. ]

It says that "a particular calling convetion, etc." is no less part of
the "language linkage" than "a particular form of representing names of
objects and functions with external linkage". That's what I said above
and you claimed to be "not correct".

Besides that, of course the code is not compatible with every C
compiler, but from what I read from the standard (Every implementation
shall provide for linkage to functions written in the C programming
language, "C", and linkage to C++ functions, "C++".), there must be at
least one C compiler that it's compatible with. How else would the
compiler be able to "provide linkage to functions written in the C
programming language"?
You'd have to ask Kernighan and Ritchie about that... For in C you
can have (or at least could have) incompatibility between calling
code and called code even within the same compilation unit; hence
the tendency of C programmers to write "int main(void)", because
simply leaving out the argument list for a function would match any
signature.


AFAIK, this is due to historical reasons. In C99, it's not true
anymore.


Keep in mind that the C++ standard stems from 1997, and that 1997 <
1999.


What does that have to do with each other?
When C++ is interfaced to C there is no way the C++ standard can
compensate, and so it does not even try.


Well, it doesn't say how the linkage looks like, but it says that the
compiler _must_ offer C linkage. Most C++ compilers also understand C
or are bundled with a C compiler, and I would be quite surprised if
they wouldn't just make their C linkage compatible with the C
compiler.


As a concrete example, Visual C++ supports a number of different
calling conventions such as "__stdcall" (which oldtimers like me think
of as Pascal) and "__cdecl" (which we think of as "C", purely because
of typical usage). If you decorate a function with 'extern "C"',
then (with this widely used compiler) that does _not_ affect the
calling convention. It affects only the external name the linker sees
(wrt. to the standard there's not necessarily a linker involved, but
this scheme is, as far as I know, completely standard-conforming.)

The crux is that there's no such thing as "the" C compiler.


Yes, that seems to be a quality-of-implementation issue. But as I said,
I'd expect from a C++ compiler that is bundled with a C compiler that
by default both use the same C "language linkage", including calling
conventions.
There are a number of different C compilers, and even using just one
compiler you can compile functions that look the same to the linker
(and so, no amount of fiddling in C++ can guarantee compatibility) but
which have different calling conventions, different sizes of int, or
whatever.


Yes. What's your point?
Achieving linking is just one of many considerations, and
furthermore, is not relevant to using a C++ function as a callback.


Look again at the OP's question. He asked about static members as
callback for a C program or library. And so it definitely is relevant.
That was my point anyway. I just answered that making the callback
function extern "C" is the most portable solution. Do you disagree
about that?


Yep.


Ok, so what would be more portable then?

Jul 19 '05 #7

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

Similar topics

0
by: vijaya | last post by:
I've to invoke a unmanaged dll fucntion in C# which uses a callback fucntion.The unmanaged dll fucntion is as follows ...
8
by: Ash | last post by:
Hello all, I am hoping this is the appropriate newsgroup for a C++ interface design question. I am trying to design an interface for a subscriber...
4
by: ma740988 | last post by:
// file sltest.h #ifndef SLTEST_H #define SLTEST_H class CallbackBase // herb shutters gotW source .. { public: virtual void operator()()...
15
by: Felix Kater | last post by:
Hi, in a given library I register callback functions with this function: bool set_callback(int index, int (*callback_function)(long)); I...
2
by: MR | last post by:
help! I have an unmanaged DLL that I do not have the source code, so i can't recompile or make changes. the DLL requires a callback function. I...
0
by: Boltar | last post by:
Hello, I am trying to use a CustomValidator control to perform client-side validation via a server callback. I have an example below that...
4
by: Edwin Gomez | last post by:
I'm a C# developer and I'm new to Python. I would like to know if the concept of Asynchronous call-backs exists in Python. Basically what I mean is...
6
by: smmk25 | last post by:
Before I state the problem, I just want to let the readers know, I am knew to C++\CLI and interop so please forgive any newbie questions. I have...
5
by: Jef Driesen | last post by:
I have a C DLL that I want to use from a C# project. The C header file contains these declarations: typedef void (*callback_t) (const unsigned...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.