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

时间:2022-03-25 22:54:19

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 ?

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

4 个解决方案

#1


9  

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. 一般的原则是每个头文件都要自己处理(并且是自给自足的)。因此,根据这个原则,不需要将头文件打包在外部“C”中,因为头文件在它们(如果需要的话)中会有一个外部“C”。因此,在当前文件中,您将把它放在包含项之后。
  3. 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".
  4. 但是如果你有一大堆的头文件,你不想添加一个外部的“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编译器编译的归档文件中查找要链接的错误名称,但无法找到它们。

#2


9  

This construct is used to make your names 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_

#3


1  

extern "C" affects the way that code is compiled. Headers that are designed to be compiled both as C and as C++ will manage extern "C" themselves. You should never wrap a #include directive 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.

外部“C”影响代码编译的方式。设计为同时作为C和c++编译的头文件将管理extern“C”本身。您不应该将#include指令封装在一个extern“C”块中:如果所涉及的header设计为以两种方式编译,则说明该指令是冗余的,而且如果它不是设计为以两种方式使用,那么它就是一个错误。

#4


1  

  • 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.

    外来的“C”影响连杆。当编译c++函数时,它们的名称会有所不同,这就是为什么在c++中可能出现重载。因此,函数名根据参数的类型和数量进行修改,因此具有相同名称的两个函数将有两个不同的符号名。

  • 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.

    外部“C”中的代码仍然是c++代码。在extern“C”块中,你可以做的事情是有局限性的,但它们都是关于链接的。

#1


9  

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. 一般的原则是每个头文件都要自己处理(并且是自给自足的)。因此,根据这个原则,不需要将头文件打包在外部“C”中,因为头文件在它们(如果需要的话)中会有一个外部“C”。因此,在当前文件中,您将把它放在包含项之后。
  3. 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".
  4. 但是如果你有一大堆的头文件,你不想添加一个外部的“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编译器编译的归档文件中查找要链接的错误名称,但无法找到它们。

#2


9  

This construct is used to make your names 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_

#3


1  

extern "C" affects the way that code is compiled. Headers that are designed to be compiled both as C and as C++ will manage extern "C" themselves. You should never wrap a #include directive 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.

外部“C”影响代码编译的方式。设计为同时作为C和c++编译的头文件将管理extern“C”本身。您不应该将#include指令封装在一个extern“C”块中:如果所涉及的header设计为以两种方式编译,则说明该指令是冗余的,而且如果它不是设计为以两种方式使用,那么它就是一个错误。

#4


1  

  • 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.

    外来的“C”影响连杆。当编译c++函数时,它们的名称会有所不同,这就是为什么在c++中可能出现重载。因此,函数名根据参数的类型和数量进行修改,因此具有相同名称的两个函数将有两个不同的符号名。

  • 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.

    外部“C”中的代码仍然是c++代码。在extern“C”块中,你可以做的事情是有局限性的,但它们都是关于链接的。