473,503 Members | 6,354 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4081
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
1954
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
2749
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
5959
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
8232
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
6135
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
4716
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
6239
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
2079
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
2914
by: Medvedev | last post by:
What's that preprocessor do #ifdef __cplusplus extern "C" { #endif .. .. .. #ifdef __cplusplus } #endif
0
7207
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7357
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7012
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5598
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5023
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
1522
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
402
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.