【发布时间】:2014-09-30 14:58:43
【问题描述】:
在阅读Why do we need extern "C"{ #include <foo.h> } in C++? 之后,我得出结论,在 extern "C" 块中包含一个标题是可以的。
但是我遇到了这个星座的问题:
#ifdef __cplusplus
extern "C" {
#endif
#include "mixedstuff.h"
#ifdef __cplusplus
}
#endif
mixedstuff.h:
#ifdef __cplusplus
extern "C" {
#endif
#include "cstuff.h"
#ifdef __cplusplus
}
#include "cppstuff.h"
#endif
正如大家所见,我最终将cppstuff.h 包含在extern "C" 块中的C++ 编译器中。这导致了很多错误,因为很多语句都不能使用 C-linkage。
显而易见的解决方案是在extern "C" 块之外添加#include mixedstuff.h。但这需要我查看每个标题,看看它是普通的 C、C++ 还是混合的。
据此,我建议使标头可以识别 C++,而不是将它们包含在 extern "C" 中。我是对的,还是有更好的方法?
【问题讨论】:
-
第一次使用 __cplusplus 是不必要的,因为包含的 .h 文件已经处理了它。这当然解决了问题。不要帮助太多:)
-
这就是我所说的“显而易见的解决方案”。我试图为这种情况寻求最佳实践。
标签: c++ c c-preprocessor