__cplusplus 编译器指令已定义和未定义

__cplusplus compiler directive defined and not defined

我在使用 eclipse CDT 时遇到问题。 我有一个使用 C FatFs 库的 C++ 项目。我正在尝试实施 fatfs 文件。 问题:在我添加的多个文件中

#ifdef __cplusplus 
extern "C" 
{ 
#endif 

// code..

#ifdef __cplusplus
}
#endif

包装器。但由于某种原因,在一个 .h 文件中定义了 _cplusplus,而在另一个 .h 文件中未定义 __cplusplus

有什么建议吗?
可以发送屏幕截图进行说明。

是否定义__cplusplus取决于包含头文件的文件是如何被编译的。如果文件被编译为 C 源 (.c),则不会定义它。如果文件被编译为 C++ 源文件(.cpp.cc 或作为 C++ 源文件关联的任何其他扩展名),则将定义 __cplusplus

仔细检查文件扩展名,必要时检查项目中的设置,以确保正确编译文件。

看这里:Combining C++ and C — how does #ifdef __cplusplus work?

extern "C" doesn't really change the way that the compiler reads the code. If your code is in a .c file, it will be compiled as C, if it is in a .cpp file, it will be compiled as C++ (unless you do something strange to your configuration).

What extern "C" does is affect linkage. C++ functions, when compiled, have their names mangled -- this is what makes overloading possible. The function name gets modified based on the types and number of parameters, so that two functions with the same name will have 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.

此外,您可能需要 两个 #ifdef __cpluspluss:

#ifdef __cplusplus 
extern "C" { 
#endif 
    // ...
#ifdef __cplusplus
}
#endif

否则,您的 C 代码将永远看不到您的定义。