问题描述
我有一个C / C ++程序,这是一个插件的Firefox。因为它是一个插件,它具有非主入口点。这些入口点需要的,否则他们得到的名字mangled.However,其他功能超载被用C编译,所以他们需要的是C ++。解决的办法是外部的C。这一点我已经想通了。
I have a C/C++ program that's a plugin to Firefox. Because it's a plugin, it has non-main entry points. Those entrypoints need to be compiled in C because otherwise they get name mangled.However, other functions are over-loaded, so they need to be C++. The solution is extern "C". That much I've already figured out.
不过,如果我为externC周围的.c文件,我得到链接错误因为C ++文件名了错位,但在.c文件的调用没有。至少我的 THINK 的这是发生了什么。
However, if I extern "C" around the .c file, I get link errors because the C++ files got name mangled, but the calls in the .c file didn't. At least I THINK that's what's happening.
解决的办法似乎是把外部的C围绕.h文件。这似乎意味着,在.h文件中声明的函数的名称都没有错位,甚至当他们在(presumably错位).c文件正在定义。
The solution APPEARS to be to put the extern "C" around the .h file. This SEEMS to mean that the names of the functions declared in the .h file aren't mangled, even when they're defined in the (presumably mangled) .c file.
但是,它并没有真正使任何意义,我为什么会工作。这是一个杂牌?我在自己设定为一个硬后来找错误?或者这是解决这个正确的方式?
However, it doesn't really make any sense to me why this would work. Is this a kludge? Am I setting myself up for a hard to find bug later? or is this the right way to solve this?
推荐答案
为externC应适用于函数原型,所以如果你有不同的原型和实施,把周围的原型外部声明。实施,提供原型是可见的,会的extern以及不缺胳膊少腿。这是不是一个错误。
Extern "C" should apply to function prototype, so if you have separate prototype and implementation, put extern declaration around prototypes. Implementation, provided prototype is visible, will be extern as well and not mangled. It is not a bug.
这篇关于如何C'S"&的extern QUOT;工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!