Home > Net >  How can I include a C header that uses a C keyword as an identifier in C ?
How can I include a C header that uses a C keyword as an identifier in C ?

Time:05-10

I've been using C and compiling with clang . I would like to include the <xcb/xkb.h> header for an X11 program I am writing.

Unfortunately this header uses explicit for some field names (such as line 727) and that is a keyword in C .

Is there anyway to deal with this?

xcb/xkb.h:

// ...

#ifdef __cplusplus
extern "C" {
#endif

// ...

typedef struct xcb_xkb_set_explicit_t {
    xcb_keycode_t keycode;
    uint8_t       explicit;
} xcb_xkb_set_explicit_t;

// ...

#ifdef __cplusplus
}
#endif

// ...

CodePudding user response:

Use a macro to rename the fields:

#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wkeyword-macro"
#endif
#define explicit explicit_
#ifdef __clang__
#pragma clang diagnostic pop
#endif

#include <xcb/xkb.h>

#undef explicit

If I remember correctly, that's illegal in standard C , but GCC, Clang (with pragma), and MSVC do accept it.

CodePudding user response:

How can I include a C header that uses a C keyword as an identifier in C ?

There is no standard conforming solution to be able to include such header. In order to be able to include a header in C , it must be written in valid C . In case of a C header, it would thus have to be written in common subset of C and C .

The ideal solution is to fix the header to be valid C . A standard conforming workaround is to write a C conforming wrapper in C.

  • Related