我如何包括使用C++的C标头。关键字作为C++的标识符?

发布于 2025-01-27 22:02:35 字数 567 浏览 3 评论 0 原文

我一直在使用C ++并用Clang ++编译。我想包括< xcb/xcb/xkb/xkb.h> 我正在写的X11程序的标题。

不幸的是,该标头使用 explicit 用于某些字段名称(例如第727行),这是C ++中的关键字。

无论如何是否有这样的处理?

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

// ...

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

// ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

赠佳期 2025-02-03 22:02:35

使用宏来重命名字段:

#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

将关键字用作宏名称为 in标准C ++ ,但是GCC,Clang(带有Pragma)和MSVC确实接受。 (标准说您“不得做”,cppReference 澄清 “不良形式”。)

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

Using a keyword as a macro name is ill-formed in standard C++, but GCC, Clang (with pragma), and MSVC do accept it. (The standard says you "shall not" do it, cppreference clarifies that it means "ill-formed".)

野生奥特曼 2025-02-03 22:02:35

如何包括使用C ++关键字作为C ++的标识符的C标头?

没有标准符合条件的解决方案可以包括此标头。为了能够在C ++中包含标头,必须以有效的C ++写入。因此,对于C标头,必须将其写入C和C ++的常见子集。

理想的解决方案是将标头固定为有效的C ++。标准配合的解决方法是在C中编写C ++符合包装纸。

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.

陪你搞怪i 2025-02-03 22:02:35

(不是对确切问题的答案,但对于连接C&amp; c ++的一般问题可能有用

)标头不兼容,并为您的其他程序提供C ++ - 兼容接口?如果该接口足够高,那么您只需要在包装器.c文件中包含不兼容的标头文件,从而允许正常C ++代码中包含包装器.H标头文件。

根据详细信息,您最终可能会进行一些数据的琐碎复制,但是所得的C接口可能更适合您的特定需求。

(Not an answer to the exact question, but possibly useful for the general problem of interfacing C & C++)

How about using an intermediate set of C files (.c & .h) to wrap the incompatible header and provide a C++-compatible interface to the rest of your program? If that interface is high-level enough, then you would only need to include the incompatible header file in the wrapper .c file only, allowing the wrapper .h header file to be included in C++ code normally.

Depending on the details, you might end up doing some trivial copying of data, but the resulting C interface might be much more suited to your specific needs.

南街女流氓 2025-02-03 22:02:35

一种解决方案是在您的makefile中添加一代步骤,以复制标题并将其命名为字段,并包括上述自动化标头。

(在一般情况下,这是难以做到的,但是在这种情况下,鉴于标题不太可能仅使用 explicit ,甚至只是一个简单的事情( gnu sed> sed s/\ bexplitic \ bexplitic \ b/explicit_field/explicit_field/ g 或homeSuch)将工作更棘手的

位置 。如果您的其他内容包括间接包含标题。

至少对于名义上兼容的标头,仅更改字段名称不应对EG ABI产生任何影响。 (在C ++中有些情况下,重命名字段确实会影响事物。)

One solution is to add a generation step to your makefile that copies the header and renames the field, and include said autogenerated header instead.

(This is between difficult to impossible to do in the general case, but in this case given that the header isn't likely using explicit for anything else even just a simple (gnu) sed (s/\bexplicit\b/explicit_field/g or somesuch) would work. The trickier bit is figuring out the correct file to copy.)

Make sure that the location of the copied header is before the original header in your include path, in case something else in your includes indirectly includes the header.

Just changing a field name shouldn't have any effects on e.g. ABI, at least for a nominally-C-compatible header. (There are cases in C++ where renaming a field does affect things.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文