为什么你需要"外部C"对于C ++回调的C函数? [英] Why do you need "extern C" for C++ callbacks to C functions?

查看:234
本文介绍了为什么你需要"外部C"对于C ++回调的C函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得在升压code这样的例子。

 名字空间boost {
   命名空间{
     为externCvoid *的thread_proxy(无效* F)
     {
       ....
     }   } //匿名
   无效线程:: thread_start(...)
   {
       ...
       在pthread_create(东西,0,&安培; thread_proxy,something_else);
       ...
   }
} //提振

为什么你真正需要这个的externC

很显然,在 thread_proxy 函数是私有的内部,我不希望它
将错位为thread_proxy因为我其实并不需要它缺胳膊少腿的。

事实上,在我写,而且运行在许多平台上的所有我的code,我从来没有使用的externC这工作过AS-与正常功能。

为什么的externC补充?


我的问题是,的externC功能污染全局命名空间,但实际上并没有隐藏的作者的期望。

这不是一个重复的!
我不是在谈论重整内外联动。正是在这样的code外部联动是不需要的明显!

答: C和C ++函数的调用约定不一定是一样的,所以你需要创建一个用C调用约定。见7.5的C ++标准(P4)。


解决方案

  

很显然,在 thread_proxy 函数是私有的内部,我不认为它会错位为thread_proxy因为我其实并不需要它缺胳膊少腿在所有


尽管如此,它仍然会被截断。 (如果不是因为的externC)这只是编译器如何工作。我同意这是可以想象的编译器可以说,这并不一定需要进行错位,但标准说就可以了什么。这就是说,识别码不来这里发挥作用,因为我们不是要链接到的功能。


  

事实上,在我写,而且运行在许多平台上的所有我的code,我从来没有使用的externC这工作过AS-与正常功能。


在不同的平台

写作无关,与的externC。我希望所有的标准C ++ code就具有标准的C ++编译器兼容所有平台上工作。

的externC具有与C,其中的pthread是库接口做。它不仅没有裂伤的名字,它可以确保它的调用与C调用约定。这是调用约定,需要得到保证,因为我们不能假定我们是在一个特定的编译器,平台或架构上运行,最好的办法,试图做的是给我们的功能:为externC


  

我的问题是,的externC功能污染全局命名空间,但实际上并没有隐藏的作者的期望。


有什么污染有关上述code。这是在一个无名的命名空间,而不是翻译单元外部访问。

I find such examples in Boost code.

namespace boost {
   namespace {
     extern "C" void *thread_proxy(void *f)
     {
       ....
     }

   } // anonymous
   void thread::thread_start(...)
   {
       ...
       pthread_create(something,0,&thread_proxy,something_else);
       ...
   }
} // boost

Why do you actually need this extern "C"?

It is clear that the thread_proxy function is private internal and I do not expect that it would be mangled as "thread_proxy" because I actually do not need it mangled at all.

In fact, in all my code that I had written and that runs on many platforms, I never used extern "C" and this had worked as-is with normal functions.

Why is extern "C" added?


My problem is that extern "C" functions pollute the global namespace and they are not actually hidden as the author expects.

This is not a duplicate! I'm not talking about mangling and external linkage. It is obvious in this code that external linkage is unwanted!

Answer: The calling conventions of C and C++ functions are not necessarily the same, so you need to create one with the C calling convention. See 7.5 (p4) of C++ standard.

解决方案

It is clear that the thread_proxy function is private internal and I do not expect that it would be mangled as "thread_proxy" because I actually do not need it mangled at all.

Regardless, it's still going to be mangled. (Had it not been extern "C") That's just how the compiler works. I agree it's conceivable a compiler could say "this doesn't necessarily need to be mangled", but the standard says nothing on it. That said, mangling doesn't come into play here, as we aren't trying to link to the function.

In fact, in all my code that I had written and that runs on many platforms, I never used extern "C" and this had worked as-is with normal functions.

Writing on different platforms has nothing to do with extern "C". I expect all standard C++ code to work on all platforms that have a standard C++ compliant compiler.

extern "C" has to do with interfacing with C, which pthread is a library of. Not only does it not mangle the name, it makes sure it's callable with the C calling convention. It's the calling convention that needs to be guaranteed, and because we can't assume we are running on a certain compiler, platform, or architecture, the best way to try and do that is with the functionality given to us: extern "C".

My problem is that extern "C" functions pollute the global namespace and they are not actually hidden as the author expects.

There's nothing polluting about the above code. It's in an unnamed namespace, and not accessible outside the translation unit.

这篇关于为什么你需要"外部C"对于C ++回调的C函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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