C和C ++调用约定之间有什么区别? [英] What are the differences between C, and C++ calling conventions?

查看:59
本文介绍了C和C ++调用约定之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,调用约定取决于编译器和体系结构.但是C和C ++调用约定之间是否有明显的区别?

解决方案

但是C和C ++调用约定之间有明显区别吗?

通常没有. C ++的目的是尽可能地与C兼容,特别是它在所有系统上都使用C应用程序二进制接口.

但是C ABI不能满足C ++所需的许多功能(特别是重载,名称空间和函数模板),因此C ++编译器对函数名称进行了一些更改.这称为名称处理

因此,为了使C和C ++代码之间的函数调用能够正常工作,必须将此类函数声明为extern "C",这将禁用名称修饰,并确保调用约定是C期望的约定(但我希望这是后一个方面)即使标准没有强制要求自动提供).

C ++还具有成员函数的附加调用约定(有时称为 解决方案

But are there any clear differences between C and C++ calling conventions?

In general, there’s none. C++ was intentionally designed to be as much as possible compatible with C, and in particular it uses the C application binary interface on all systems.

But the C ABI doesn’t cater for a lot of features that C++ needs (in particular, overloading, namespaces and function templates) so the C++ compiler makes some changes to the names of functions. This is called name mangling

So in order for function calls between C and C++ code to work, such functions must be declared as extern "C" which disables name mangling and makes sure that the calling conventions are those expected by C (but I expect that this latter aspect comes automatically, even though the standard doesn’t mandate this).

C++ also has additional calling conventions for member functions (sometimes called thiscall) which don’t exist in C. But free functions use the same calling convention as C, whichever that is on a given system.

这篇关于C和C ++调用约定之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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