【问题标题】:When to use extern "C" in C++? [duplicate]何时在 C++ 中使用外部“C”? [复制]
【发布时间】:2010-11-20 11:58:32
【问题描述】:

可能重复:
Why do we need extern “C”{ #include <foo.h> } in C++?

我经常看到这样编码的程序:

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

为什么我们使用extern "C" 块?我们可以用 C++ 中的东西替换它吗?使用extern "C"有什么好处吗?

我确实看到了一个解释 this 的链接,但是当我们已经有了 C++ 时,为什么还需要用 C 编译一些东西呢?

【问题讨论】:

标签: c++ c extern-c


【解决方案1】:

extern "C" 使名称不被破坏。

在以下情况下使用:

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

    extern "C" int foo(int);
    
  2. 我们需要将一些 C++ 代码导出到 C

    extern "C" int foo(int) { something; }
    
  3. 我们需要能够解析共享库中的符号——所以我们需要摆脱重整

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

【讨论】:

    【解决方案2】:

    extern "C" 有意义的一个地方是当您链接到编译为 C 代码的库时。

    extern "C" {
      #include "c_only_header.h"
    }
    

    否则,您可能会收到链接器错误,因为该库包含具有 C 链接 (_myfunc) 的函数,但 C++ 编译器将库的标头作为 C++ 代码处理,为函数生成了 C++ 符号名称 ("_myfunc@XAZZYE" - 这称为重整,每个编译器都不同)。

    另一个使用 extern "C" 的地方是保证 C 链接,即使是用 C++ 编写的函数,例如。

    extern "C" void __stdcall PrintHello() {
      cout << "Hello World" << endl;
    }
    

    这样的函数可以导出到 DLL,然后可以从其他编程语言调用,因为编译不会破坏它的名称。如果您添加了同一函数的另一个重载,例如。

    extern "C" void __stdcall PrintHello() {
      cout << "Hello World" << endl;
    }
    extern "C" void __stdcall PrintHello(const char *name) {
      cout << "Hello, " << name << endl;
    }
    

    大多数编译器会捕捉到这一点,从而阻止您在 DLL-public 函数中使用函数重载。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-17
      • 2012-05-11
      • 1970-01-01
      • 2010-10-15
      • 1970-01-01
      • 2013-07-02
      相关资源
      最近更新 更多