当为extern&QUOT使用; C"在C ++? [英] When to use extern "C" in C++?

查看:175
本文介绍了当为extern&QUOT使用; C"在C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  我们为什么需要的extern“ C”的{ #包括LT&;&foo.h中GT; }在C ++?

我经常看到程序codeD这样的:

I have often seen programs coded like:

extern "C" bool doSomeWork() {
  //
  return true;
}

为什么要使用的externC阻止?我们可以在C ++的东西代替吗?是否有任何优势,使用的externC

Why do we use an extern "C" block? Can we replace this with something in C++? Is there any advantage to using extern "C"?

我看到一个链接解释rel=\"nofollow\">这

I do see a link explaining this but why do we need to compile something in C when we already have C++?

推荐答案

为externC使得名未进行重整。

extern "C" makes names not mangled.

它适用于:


  1. 我们需要在C中使用一些C ++库

  1. We need to use some C library in C++

extern "C" int foo(int);


  • 我们需要输出一些C ++ code到C

  • We need export some C++ code to C

    extern "C" int foo(int) { something; }
    


  • 我们需要共享库来解决符号的能力 - 所以我们需要摆脱重整

  • We need an ability to resolve symbol in shared library -- so we need to get rid mangling

    extern "C" int foo(int) { something; }
    ///
    typedef int (*foo_type)(int);
    foo_type f = (foo_type)dlsym(handle,"foo")
    


  • 这篇关于当为extern&QUOT使用; C"在C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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