放置#ifdef __cplusplus extern "C" { #endif 的最佳位置在哪里

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16087451/
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 20:03:58  来源:igfitidea点击:

Where is the best place to put the #ifdef __cplusplus extern "C" { #endif

c++cinclude

提问by Vincent

I would like to know where is better to put the

我想知道放在哪里比较好

#ifdef __cplusplus
extern "C" {
#endif

in a C header file.

在 C 头文件中。

At the beginning or after all the other includes. why ?

在开始或之后所有其他包括。为什么 ?

回答by Ziffusion

There are no strict rules on this, but note the following.

对此没有严格的规定,但请注意以下几点。

  1. The general principle is that each header file takes care of itself (and is self sufficient). So, by this principle, there would be no need to wrap the header files in a extern "C", because the header files would have an extern "C" in them (if they need one). So, in the current file, you would place it after the other includes.
  2. But if you do a have a whole bunch of headers, that you don't want to add an extern "C" to, and want to make available through a single include, by all means, go ahead and wrap them up in a file wide extern "C".
  1. 一般原则是每个头文件都自己处理(并且是自给自足的)。因此,根据这一原则,不需要将头文件包装在 extern "C" 中,因为头文件中会有一个 extern "C"(如果需要的话)。因此,在当前文件中,您会将它放在另一个包含之后。
  2. 但是如果你有一大堆头文件,你不想向其中添加一个 extern "C",并且希望通过单个包含使其可用,无论如何,请继续将它们包装在一个文件中宽外部“C”。

Just know that the idea behind extern "C" is that it makes the compiler generate C friendly linkage. Otherwise, code compiled with a C++ compiler looks for mangled names to link against in archives compiled with a C compiler, and can't find them.

只知道 extern "C" 背后的想法是它使编译器生成 C 友好链接。否则,用 C++ 编译器编译的代码会在用 C 编译器编译的档案中查找要链接的损坏名称,但找不到它们。

回答by Offirmo

This construct is used to make yournames available to a C linker (short explanation)

此构造用于使您的名称可用于 C 链接器(简短说明)

So obviously you want to use it around your stuff only.

所以很明显你只想在你的东西周围使用它。

Like this :

像这样 :

#ifndef MY_INCLUDE_H_ // include guard
#define MY_INCLUDE_H_

#include <...> // dependencies
#include "..."

#ifdef __cplusplus
extern “C” {
#endif

// ... your types, methods, variables

#ifdef __cplusplus
}
#endif

#endif // MY_INCLUDE_H_

回答by SD.

  • extern "C" affects linkage. When C++ functions compiled they have their names varies, that's why overloading in C++ is possible. So, function name gets modified based on the types and number of parameters, so two functions with the same names will have two different symbol names.

  • Code inside an extern "C" is still C++ code. There are limitations on what you can do in an extern "C" block, but they're all about linkage.

  • extern "C" 影响链接。编译 C++ 函数时,它们的名称各不相同,这就是为什么可以在 C++ 中重载的原因。因此,函数名称会根据参数的类型和数量进行修改,因此具有相同名称的两个函数将具有两个不同的符号名称。

  • extern "C" 中的代码仍然是 C++ 代码。在 extern "C" 块中可以做什么是有限制的,但它们都是关于链接的。

回答by Pete Becker

extern "C"affects the way that code is compiled. Headers that are designedto be compiled both as C and as C++ will manage extern "C"themselves. You should neverwrap a #includedirective in an extern "C"block: if the header involved was designed to be compiled both ways your directive is redundant, and if it wasn't designed to be used both ways it's an error.

extern "C"影响代码的编译方式。被头设计既为C和C ++将管理编译extern "C"自己。你永远不应该将#include指令包装在一个extern "C"块中:如果所涉及的头文件被设计为以两种方式编译你的指令是多余的,如果它不是被设计为双向使用它是一个错误。