472,370 Members | 2,466 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

extern "C" directive and calling conventions

Hi everyone, (Very Sorry, if this is the wrong group in which I am
posting this query).

Code snippet:

//C library
typedef int (*PFunc)(int* aArg);

void call_c_foo(PFunc aPtrtoFunc)
{
int* theArg;
//Assume theArg pointer is valid hereafter
//Do something...and then...call
PFunc(theArg);
//some more stuff
}

================================

//C++ Code using C library

class CPP
{
public:
static int Ditto(int* aInt);
void InMemberFunction()
{
call_c_foo(Ditto);
}
};

=================================

On URL:
http://www.parashift.com/c++-faq-lit....html#faq-33.2
{
although it probably works on most compilers, it actually would have to
be an extern "C" non-member function to be correct, since "C linkage"
doesn't only cover things like name mangling, but also calling
conventions, which might be different between C and C++.
}

3 QUESTIONS:

1) What does Standard C++ say on this issue? Does the static fucntion
in class CPP is compiled by _most_ of compilers the way it would have
been compiled if it was given extern "C" linkage? In other words, do
_most_ of compilers treat both - static fucntion directive and extern
"C" directive - to be same? Also, why does C++ not allow to declare
linkage of a class to be partly as C and partly as C++? What are the
difficulties in doing this on part of a compiler writer?

2) When I say that a given piece of code (in C++) has "C" linkage, does
it mean that C++ compiler will "supress" its calling conventions as
well when setting up a C call within C++ code while compiling? More
curiously, what all things does a C++ compiler do when it encounters
extern "C" directive while compiling a C++ code?

3) I would have assumed that calling conventions for both extern "C"
linked calls and C++ calls would be same _if_ both the C code (in our
case - the above C library) and C++ code is compiled by _same_
compiler. Is my understanding true?

Thank you very much for the answers in advance,
-Viren

Aug 25 '05 #1
13 3935
Ian
RainBow wrote:

3 QUESTIONS:

1) What does Standard C++ say on this issue? Does the static fucntion
in class CPP is compiled by _most_ of compilers the way it would have
been compiled if it was given extern "C" linkage? In other words, do
_most_ of compilers treat both - static fucntion directive and extern
"C" directive - to be same? Also, why does C++ not allow to declare
linkage of a class to be partly as C and partly as C++? What are the
difficulties in doing this on part of a compiler writer?
No, it will be a C++ function and have its name mangled. The standard
says nothing about calling conventions.
2) When I say that a given piece of code (in C++) has "C" linkage, does
it mean that C++ compiler will "supress" its calling conventions as
well when setting up a C call within C++ code while compiling? More
curiously, what all things does a C++ compiler do when it encounters
extern "C" directive while compiling a C++ code?
In practice, it turns of name mangling so the function name will be
exported "as is".
3) I would have assumed that calling conventions for both extern "C"
linked calls and C++ calls would be same _if_ both the C code (in our
case - the above C library) and C++ code is compiled by _same_
compiler. Is my understanding true?

For every implementation I have seen, yes. It wouldn't be of any use
otherwise. In practice, it will also work with any other C compiler on
the same platform.

Ian
Aug 25 '05 #2
> 3) I would have assumed that calling conventions for both extern "C"
linked calls and C++ calls would be same _if_ both the C code (in our
case - the above C library) and C++ code is compiled by _same_
compiler. Is my understanding true?

For every implementation I have seen, yes. It wouldn't be of any use
otherwise. In practice, it will also work with any other C compiler on

the same platform.

I am actually thinking now as to how does a C++ compiler treat a class
member function versus a static member function. E.g VC++ might be
treating a class member function differently when setting up a call
(they use "this calling convention") versus a static member function
(do not use "this" calling convention here). If this is true, then VC++
is actually using 2 calling conventions for same compilation unit :
this and normal __std for static functions (that is also used for C
calls).

I am sure my understanding is completely screwed up here, but I am very
curious to learn more on this topic. I really need to write some
"portable" code and lack of this understanding is giving me restless
nights.

-Viren

Aug 25 '05 #3
Ian
RainBow wrote:
3) I would have assumed that calling conventions for both extern "C"
linked calls and C++ calls would be same _if_ both the C code (in our
case - the above C library) and C++ code is compiled by _same_
compiler. Is my understanding true?
For every implementation I have seen, yes. It wouldn't be of any use
otherwise. In practice, it will also work with any other C compiler on

the same platform.

I am actually thinking now as to how does a C++ compiler treat a class
member function versus a static member function. E.g VC++ might be
treating a class member function differently when setting up a call
(they use "this calling convention") versus a static member function
(do not use "this" calling convention here). If this is true, then VC++
is actually using 2 calling conventions for same compilation unit :
this and normal __std for static functions (that is also used for C
calls).

Don't get too caught up in this, how the compiler generates C++ member
and static member calls is its business, don't worry about it. No two
C++ compilers generate code that can be used with another.

That's one of the reasons to use extern "C", to get a portable interface
between say VC++ and gcc.
I am sure my understanding is completely screwed up here, but I am very
curious to learn more on this topic. I really need to write some
"portable" code and lack of this understanding is giving me restless
nights.

If you want to see what your compiler does under the hood, find the
option that emits an assembler file and study that.

Ian
Aug 25 '05 #4
Ian wrote:
RainBow wrote:

3 QUESTIONS:

1) What does Standard C++ say on this issue? Does the static fucntion
in class CPP is compiled by _most_ of compilers the way it would have
been compiled if it was given extern "C" linkage? In other words, do
_most_ of compilers treat both - static fucntion directive and extern
"C" directive - to be same?
Most seem to do, yes, but that is an error. They are not supposed to.
Also, why does C++ not allow to declare linkage of a class to be partly
as C and partly as C++? What are the difficulties in doing this on part
of a compiler writer?

No, it will be a C++ function and have its name mangled. The standard
says nothing about calling conventions.


That's not true. The C++ standard says that calling conventions can be part
of "linkage" and thus, an extern "C" function is not guaranteed to be
callable through a pointer to a function that is not.

Aug 25 '05 #5
Ian wrote:
In practice, it turns of name mangling so the function name will be
exported "as is".


In practice extern "C" applies the same mangling as the targetted C
compiler. Often that means adding an underscore at the beginning of the
name; sometimes it means using the same name; sometimes it means adding
an underscore at the end. Or it could mean something completely different.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Aug 25 '05 #6
RainBow wrote:
3) I would have assumed that calling conventions for both extern "C"
linked calls and C++ calls would be same if both the C code (in our
case - the above C library) and C++ code is compiled by same
compiler. Is my understanding true?

For every implementation I have seen, yes. It wouldn't be of any use
otherwise. In practice, it will also work with any other C compiler
on

It looks to me like you want to quote while using Google, but don't
know how, so you did a cut and paste from the previous message.

To get proper quoting and attributions from Google, click "show
options" and use the Reply shown in the expanded header.


Brian
Aug 25 '05 #7
If it is allowed/legal, can you please dump the relevant paragraph from
Standards here so that I get a chance to read exactly what does the
standard say? I will need to explain my design during the review, so
this quote from Standards will come handy. Sorry, I /my company does
not have a copy of current C++ standards.

Thanks and regards.

Aug 25 '05 #8
:-) I know I should be doing that but I am really running out of time
to do this R&D. Sorry.

Aug 25 '05 #9
Ian
Rolf Magnus wrote:
Ian wrote:

RainBow wrote:
3 QUESTIONS:

1) What does Standard C++ say on this issue? Does the static fucntion
in class CPP is compiled by _most_ of compilers the way it would have
been compiled if it was given extern "C" linkage? In other words, do
_most_ of compilers treat both - static fucntion directive and extern
"C" directive - to be same?

Most seem to do, yes, but that is an error. They are not supposed to.

Also, why does C++ not allow to declare linkage of a class to be partly
as C and partly as C++? What are the difficulties in doing this on part
of a compiler writer?


No, it will be a C++ function and have its name mangled. The standard
says nothing about calling conventions.

That's not true. The C++ standard says that calling conventions can be part
of "linkage" and thus, an extern "C" function is not guaranteed to be
callable through a pointer to a function that is not.

Agreed, I should have said "The standard says nothing about calling
convention implementation".

Ian
Aug 25 '05 #10
RainBow wrote:
:-) I know I should be doing that but I am really running out of time
to do this R&D. Sorry.

Others don't seem to be bothered by this lack of quoting, but it's
bugging me. To get proper quotes and attributions from Google, click
"show options" and use the Reply shown in the expanded header.


Brian
Aug 25 '05 #11
Ian
RainBow wrote:
If it is allowed/legal, can you please dump the relevant paragraph from
Standards here so that I get a chance to read exactly what does the
standard say? I will need to explain my design during the review, so
this quote from Standards will come handy. Sorry, I /my company does
not have a copy of current C++ standards.

Is what legal? If you want decent replies, please quote correctly.

Have a read of section 7.4, please don't ask people to quote copyright
material, it only costs $18 to download.

Ian
Aug 25 '05 #12
I am sorry - I did not mean to offend anyone. By legal, I meant if it
is permitted by this group or by your company. And I am sorry again, I
was not knowing that the cost is $18 - my guess was it must be
something in range of $500 soemthing.

Lastly, please try to understand that not everyone who visits/posts in
this group has English as their first language. So, it is very much
possible that people may just use some wrong word in wrong context -
but believe me, they dont mean what it actually looks like to an
English expert.

In apology again.

Aug 25 '05 #13
Ian
RainBow wrote:
I am sorry - I did not mean to offend anyone. By legal, I meant if it
is permitted by this group or by your company. And I am sorry again, I
was not knowing that the cost is $18 - my guess was it must be
something in range of $500 soemthing.

Lastly, please try to understand that not everyone who visits/posts in
this group has English as their first language. So, it is very much
possible that people may just use some wrong word in wrong context -
but believe me, they dont mean what it actually looks like to an
English expert.

In apology again.

No need, but please quote when responding, that's why I wasn't sure if
you were referring to something I had said being legal C++!

Ian
Aug 25 '05 #14

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

Similar topics

3
by: Christopher M. Lusardi | last post by:
Hello, THE PROBLEM ----------- If I compile parts of my program with CC, and C, using extern "C" as appropriate it compiles without any errors, but it does a segmentation fault when I run the...
4
by: cpptutor2000 | last post by:
Could some C++ guru help me please? I am trying to build an application using gcc 3.2.3, that has a some classes using functions defined in some C files in the same directory. If inside the C++...
22
by: Ian | last post by:
The title says it all. I can see the case where a function is to be called directly from C, the name mangling will stuff this up. But I can't see a reason why a template function can't be...
9
by: tropostropos | last post by:
On Solaris, using the Sun compiler, I get annoying warnings from the following code. The problem is that I am passing a C++ member function pointer to the C library function qsort. Is there a...
10
by: Mark A. Gibbs | last post by:
I have a question about mixing C and C++. In a C++ translation unit, I want to define a function with internal linkage and C calling convention. Here's a sample of what I want to do: //...
4
by: kk_oop | last post by:
Hi. I need to write a C++ callback function and register it with a C program. I've read that this can be done if the callback function is a static method. I've also read that I should use a...
4
by: mimi | last post by:
The programmer indicates to the compiler that a function is written in a different programming language using a linkage directives.It is intuitive that extern "SomeLanguage" is used to declare...
3
by: sarang | last post by:
hi, i m sarang the new member of 'thescripts'. i want to know how to use extern directive with function. i can better explain my problem in following way: I saw in one of the project, there was...
5
by: Medvedev | last post by:
What's that preprocessor do #ifdef __cplusplus extern "C" { #endif .. .. .. #ifdef __cplusplus } #endif
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
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 credentials and received a successful connection...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
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 synthesis of my design into a bitstream, not the C++...
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. Background colors can be used to highlight important...
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 starter kit that's not only easy to use but also...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.

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.