已经有extern "C"关于函数的问题,但是这个问题试图将其扩展到其他事物,例如变量等等。
如果我有一个foo.hpp使用foo.cC++ 文件的标头,我制作了这个模板,并为这个问题填充了简单的示例:
/******************************************************************************
******* include guard { ******************************************************
******************************************************************************/
#ifndef FOO_HPP
#define FOO_HPP
/******************************************************************************
******* headers **************************************************************
******************************************************************************/
#include <cstdbool>
#include <cstddef>
#include <cstdint>
/******************************************************************************
******* typedefs *************************************************************
******************************************************************************/
/* `#if !defined(UINT128_MAX)` is to test if uint128_t already exists */
#if !defined(UINT128_MAX)
typedef __uint128_t uint128_t;
typedef __int128_t int128_t;
#endif
/******************************************************************************
******* macros ***************************************************************
******************************************************************************/
#if !defined(UINT128_MAX)
#define UINT128_MAX (~((uint128_t)0))
#endif
#if !defined(INT128_MAX)
#define INT128_MAX ((int128_t)(UINT128_MAX >> 1))
#endif
#if !defined(INT128_MIN)
#define INT128_MIN ((int128_t)(-INT128_MAX - 1))
#endif
/******************************************************************************
******* enums ****************************************************************
******************************************************************************/
enum Some_Enum {
SOME_CONSTANT_A,
SOME_CONSTANT_B,
SOME_CONSTANT_C
};
/******************************************************************************
******* structs / unions *****************************************************
******************************************************************************/
union Some_Union {
int128_t a;
int64_t b[SOME_CONSTANT_C];
};
struct Some_Struct {
union Some_Union a;
bool b;
};
/******************************************************************************
******* static const variables ***********************************************
******************************************************************************/
static const struct Some_Struct x = {
.a = {
.b = {
[SOME_CONSTANT_A] = 0,
[SOME_CONSTANT_B] = 1
}
},
.b = true
};
/******************************************************************************
******* C wrapper { **********************************************************
******************************************************************************/
extern "C" {
/******************************************************************************
******* extern variables *****************************************************
******************************************************************************/
extern union Some_Union y;
/******************************************************************************
******* extern functions *****************************************************
******************************************************************************/
int foo(size_t n, int128_t arr[restrict]);
/******************************************************************************
******* } C wrapper **********************************************************
******************************************************************************/
} /* extern "C" */
/******************************************************************************
******* static inline functions (prototypes) *********************************
******************************************************************************/
static inline int compare_ldbl(const void *a_p, const void *b_p);
/******************************************************************************
******* static inline functions (definitions) ********************************
******************************************************************************/
static inline int compare_ldbl(const void *a_p, const void *b_p)
{
long double a = *(long double *)a_p;
long double b = *(long double *)b_p;
if (a < b)
return -1;
else if (a > b)
return 1;
else
return 0;
}
/******************************************************************************
******* } include guard ******************************************************
******************************************************************************/
#endif /* foo.hpp */
/******************************************************************************
******* end of file **********************************************************
******************************************************************************/
所有类型、宏、enums、structs、unions 以及extern变量和函数都应该与 C 兼容(尽可能)。只有static事情可以表现不同,因为我可以在标题中为 C++ 调整它们。
那是放置 C 包装器的正确位置吗?