何时在 C++ 中使用 extern "C"?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1292138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:27:39  来源:igfitidea点击:

When to use extern "C" in C++?

c++cextern-c

提问by pankajt

Possible Duplicate:
Why do we need extern “C”{ #include <foo.h> } in C++?

可能的重复:
为什么我们需要在 C++ 中使用 extern “C”{ #include <foo.h> }?

I have often seen programs coded like:

我经常看到程序编码如下:

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

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

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

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

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

回答by Artyom

extern "C" makes names not mangled.

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

It used when:

它在以下情况下使用:

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

    extern "C" int foo(int);
    
  2. We need export some C++ code to C

    extern "C" int foo(int) { something; }
    
  3. 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")
    
  1. 我们需要在 C++ 中使用一些 C 库

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

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

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

回答by Cygon

One place where extern "C" makes sense is when you're linking to a library that was compiled as C code.

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

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

Otherwise, you might get linker errors because the library contains the functions with C-linkage (_myfunc) but the C++ compiler, which processed the library's header as C++ code, generated C++ symbol names for the functions ("_myfunc@XAZZYE" - this is called mangling and different for each compiler).

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

Another place where extern "C" is used is to guarantee C linkage even for functions written in C++, eg.

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

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

Such a function can be exported to a DLL and will then be callable from other programming language because the compile will not mangle its name. If you added another overload of the same function, eg.

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

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

Most compilers would then catch this and thus prevent you from using function overloads in your DLL-public functions.

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