我正在研究一些应该在 C 和 C++ 中使用的库头文件。由于 C 没有命名空间概念,我将在头文件中定义的所有名称中添加“库前缀”。相比之下,对于 C++,我会定义一个命名空间。所以我目前认为设计是这样的:
mylib.h
#ifdef __cplusplus
namespace mylib{
#define NAME_PREFIX(identifier) identifier
extern "C" {
#else
#define NAME_PREFIX(identifier) mylib_identifier
#endif
int NAME_PREFIX(foo)(void);
//other declarations go here
#ifdef __cplusplus
} //extern "C"
} //namespace mylib
#endif
我从来没有见过这样的事情,所以我不确定这是否普遍。是否不鼓励这样做?可能的缺点是什么?