外部“C”指令和调用约定 [英] extern "C" directive and calling conventions

查看:80
本文介绍了外部“C”指令和调用约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,(非常抱歉,如果这是一个错误的群组,我发布此查询是
)。


代码片段:


// C库

typedef int(* PFunc)(int * aArg);


void call_c_foo(PFunc aPtrtoFunc)

{

int * theArg;

//以下假设指针有效

// Do什么...然后......打电话

PFunc(theArg);

//更多东西

}
< br $>
================================


//使用C库的C ++代码


class CPP

{

public:

static int Ditto(int * aInt);

void InMemberFunction()

{

call_c_foo(Ditto);

}

};


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


关于网址:
http://www.parashift.com/c ++ - faq-lit ... .html#faq-33.2

{

虽然它可能适用于大多数编译器,但实际上它必须是

是一个外部C非成员函数是正确的,因为C link

不仅涵盖了名称修改等内容,而且还调用了

约定,这可能是不同的在C和C ++之间。

}


3问题:


1)标准C ++对此有何评价问题? CPP类中的静态函数是否由_most_编译器编译,如果它被赋予externC,它将被编译为b $ b。连锁?换句话说,执行

_most_编译器同时处理 - 静态指令和外部指令

" C"指令 - 是一样的吗?另外,为什么C ++不允许声明一个类的部分为C和部分为C ++的
链接?在编译器编写器上执行此操作有什么困难?


2)当我说某段代码(在C ++中)有" C"联系,是吗?
这意味着C ++编译器将压制在编译时在C ++代码中设置C调用时,它的调用约定为

?更多

奇怪的是,C ++编译器在遇到它时所做的所有事情

externC编译C ++代码时的指令?


3)我会假设externC

链接调用和C ++调用的调用约定将是同样_if_ C代码(在我们的

情况 - 上面的C库中)和C ++代码由_same_

编译器编译。我的理解是否正确?


非常感谢你提前给出答案,

-Viren

解决方案

RainBow写道:


3问题:

1)标准C ++在这个问题上有什么说法?类CPP中的静态函数是否由编译器编译,如果它被赋予externC,它将被编译。连锁?换句话说,编译器是否同时处理 - 静态指令和外部指令
C指令。指令 - 是一样的吗?另外,为什么C ++不允许将类的链接声明为部分为C,部分为C ++?在编译器编写器的一部分上执行此操作有什么困难?

不,它将是一个C ++函数并且其名称受损。标准

没有说出调用约定。

2)当我说一段代码(用C ++)有C时联系,是否意味着C ++编译器将压制在编译时在C ++代码中设置C调用时,它的调用约定是什么?更奇怪的是,C ++编译器在遇到外部C时会做些什么呢?编译C ++代码时的指令?

实际上,它会改变名称,因此函数名称将被导出为b $ b导出,按原样。

3)我会假设externC链接调用和C ++调用的调用约定与C代码(在我们的
情况下 - 上面的C库)和C ++代码由_same_
编译器编译。我的理解是真的吗?



对于我见过的每一个实现,是的。它没有任何用处

否则。在实践中,它也可以在同一平台上的任何其他C编译器上工作。


Ian


> 3)我会假设externC

链接调用和C ++调用的调用约定与C代码相同_(在我们的案例中 - 上面的C库)和C ++代码由_same_
编译器编译。我的理解是真的吗?



对于我见过的每一个实现,是的。它没有任何用处

否则。在实践中,它也可以与同一平台上的任何其他C编译器一起使用。


我现在正在考虑如何做C ++编译器将一个类

成员函数与静态成员函数进行对比。例如VC ++可能是

在设置调用时对待类成员函数的方式不同

(他们使用此调用约定)与静态成员函数相比

(请勿在此处使用this调用约定)。如果这是真的,那么VC ++

实际上对同一个编译单元使用2个调用约定:

这个和普通的__std用于静态函数(也用于C

电话)。


我相信我的理解完全搞砸了,但我非常感谢b $ b好​​奇地想了解更多信息话题。我真的需要写一些

" portable"代码和缺乏这种理解让我感到焦躁不安

之夜。


-Viren


RainBow写道:

3)我会假设externC链接调用和C ++调用的调用约定是相同的_if_ C代码(在我们的案例中 - 上面的C库)和C ++代码由_same_
编译器编译。我的理解是否正确?

对于我见过的每一个实现,是的。否则它不会有任何用处。在实践中,它也可以与同一平台上的任何其他C编译器一起使用。

我现在正在考虑C ++编译器如何处理类
成员函数与静态成员函数。例如,VC ++可能会在设置调用时使用不同的方法处理类成员函数
(他们使用此调用约定)而不是静态成员函数
(不要使用this;在这里召集会议)。如果这是真的,那么VC ++实际上对同一个编译单元使用2个调用约定:
这个和普通的__std用于静态函数(也用于C
调用)。


不要太在意这个,编译器如何生成C ++成员

和静态成员调用是它的业务,不用担心。没有两个

C ++编译器生成可以与另一个一起使用的代码。


这是使用externC的原因之一,得到一个便携式界面

介于VC ++和gcc之间。

我相信我的理解完全搞砸了,但我非常好奇了解更多信息这个话题。我真的需要写一些
便携式代码和缺乏这种理解给了我不安的夜晚。



如果你想看看你的编译器做什么,找到

发出汇编文件并进行研究的选项。


Ian


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

解决方案

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


> 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


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


这篇关于外部“C”指令和调用约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆