我想知道哪里更好
#ifdef __cplusplus
extern "C" {
#endif
Run Code Online (Sandbox Code Playgroud)
在C头文件中.
在开始或之后所有其他包括.为什么?
Zif*_*ion 12
对此没有严格的规定,但请注意以下内容.
只知道extern"C"背后的想法是它使编译器生成C友好链接.否则,使用C++编译器编译的代码会查找在使用C编译器编译的存档中链接的错位名称,并且无法找到它们.
Off*_*rmo 11
此构造用于使您的名称可用于C链接器(简短说明)
显然你只想在你的东西周围使用它.
像这样 :
#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_
Run Code Online (Sandbox Code Playgroud)