问题描述
我有一个标头,用于从DLL中导出某些方法,该方法可以从C和C ++代码中使用:
#ifdef __cplusplus
extern "C"
{
#endif
API_EXPORT uint32_t __cdecl GetSomeValue();
#ifdef __cplusplus
}
#endif
对于uint32_t,我需要包含一个标头,但是哪个是正确的?
选项1:
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdint.h>
选项2:
#ifdef __cplusplus
extern "C"
{
#endif
#include <cstdint>
选项3:两者。
#ifdef __cplusplus
extern "C"
{
#include <cstdint>
#else
#include <stdint.h>
#endif
解决方法
您...只需添加标题即可。
#include <stdint.h>
#ifdef __cplusplus
extern "C"
{
#endif
API_EXPORT uint32_t __cdecl GetSomeValue();
#ifdef __cplusplus
}
#endif
您不需要在内置标头周围使用extern "C" {}
。 (有时您可能会找到需要它的库)
您的选项2通常不能与C编译器一起使用,所以可以了。您的选项1或@ user253751的答案中介绍的变体应该没问题,但是如果您考虑防止bug的C,C ++或标准库实现,那么我会在选项3中使用这种变体:
#ifdef __cplusplus
#include <cstdint>
extern "C"
{
#else
#include <stdint.h>
#endif