I have a header for exporting some methods from a DLL which can be used both from C and C++ code:
#ifdef __cplusplus
extern "C"
{
#endif
API_EXPORT uint32_t __cdecl GetSomeValue();
#ifdef __cplusplus
}
#endif
and for uint32_t, I need to include a header, but which one is correct?
Option 1: <stdint.h>
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdint.h>
Option 2:
#ifdef __cplusplus
extern "C"
{
#endif
#include <cstdint>
Option 3: both.
#ifdef __cplusplus
extern "C"
{
#include <cstdint>
#else
#include <stdint.h>
#endif
You... just include the header.
#include <stdint.h>
#ifdef __cplusplus
extern "C"
{
#endif
API_EXPORT uint32_t __cdecl GetSomeValue();
#ifdef __cplusplus
}
#endif
You don't need to use extern "C" {}
around built-in headers. (Sometimes you might find libraries that do need it)
Your option 2 generally will not work in conjunction with C compilers, so that's out. Your option 1, or the variation presented in @user253751's answer, should be fine, but if you are concerned with protecting against buggy C, C++, or standard library implementations then I would go with this variation on your option 3:
#ifdef __cplusplus
#include <cstdint>
extern "C"
{
#else
#include <stdint.h>
#endif
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.