【发布时间】:2021-05-18 14:17:14
【问题描述】:
问题
使用 GObject 和 C,我试图在模块“Foo”中创建名为“Bar”的 GObject 类的子类。但是,宏“G_DECLARE_FINAL_TYPE”(在 gobject/gtype.h 中定义)扩展不正确。它没有扩展为 FOO_BAR,而是扩展为空。所以不要扩展到:
... FooBar * FOO_BAR (gpointer ptr) { ...
它改为扩展为:
... FooBar * (gpointer ptr) { ...
错误信息
我希望它可以编译,但 gcc 给出了语法错误:
In file included from /usr/include/glib-2.0/gobject/gobject.h:24,
from /usr/include/glib-2.0/gobject/gbinding.h:29,
from /usr/include/glib-2.0/glib-object.h:22,
from foo-bar.h:4,
from foo-bar.c:1:
/usr/include/glib-2.0/gobject/gtype.h:1407:77: error: expected ‘)’ before ‘ptr’
1407 | USED static inline ModuleObjName * MODULE##_##OBJ_NAME (gpointer ptr) { \
| ^~~
foo-bar.h:10:1: note: in expansion of macro ‘G_DECLARE_FINAL_TYPE’
10 | G_DECLARE_FINAL_TYPE (FooBar, foo_bar, FOO, BAR, GObject)
| ^~~~~~~~~~~~~~~~~~~~
宏定义
这个定义来自 glib 2.66.7 库。我自己没有定义。为了完整起见,我将其包括在内。
#define G_DECLARE_FINAL_TYPE(ModuleObjName, module_obj_name, MODULE, OBJ_NAME, ParentName) \
GType module_obj_name##_get_type (void); \
G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
typedef struct _##ModuleObjName ModuleObjName; \
typedef struct { ParentName##Class parent_class; } ModuleObjName##Class; \
\
_GLIB_DEFINE_AUTOPTR_CHAINUP (ModuleObjName, ParentName) \
G_DEFINE_AUTOPTR_CLEANUP_FUNC (ModuleObjName##Class, g_type_class_unref) \
\
G_GNUC_UNUSED static inline ModuleObjName * MODULE##_##OBJ_NAME (gpointer ptr) { \
return G_TYPE_CHECK_INSTANCE_CAST (ptr, module_obj_name##_get_type (), ModuleObjName); }\
G_GNUC_UNUSED static inline gboolean MODULE##_IS_##OBJ_NAME (gpointer ptr) { \
return G_TYPE_CHECK_INSTANCE_TYPE (ptr, module_obj_name##_get_type ()); } \
G_GNUC_END_IGNORE_DEPRECATIONS
部分预处理器输出
我包含了包含语法错误的定义并对其进行了格式化以提高可读性:
__attribute__((__unused__)) static inline FooBar * (gpointer ptr) {
return (((FooBar*) g_type_check_instance_cast ((GTypeInstance*) (ptr), (foo_bar_get_type ()))));
}
奇怪的是,下面的定义确实得到了正确的预处理:
__attribute__((__unused__)) static inline gboolean FOO_IS_BAR (gpointer ptr) {
return ((__extension__ ({
GTypeInstance *__inst = (GTypeInstance*) (ptr);
GType __t = (foo_bar_get_type ());
gboolean __r;
if (!__inst) __r = (0);
else if (__inst->g_class && __inst->g_class->g_type == __t) __r = (!(0));
else __r = g_type_check_instance_is_a (__inst, __t); __r; })));
}
完整的最小示例
我在下面提供了一个导致我的错误的最小示例。
foo-bar.h
#ifndef FOO_BAR
#define FOO_BAR
#include <glib-object.h>
G_BEGIN_DECLS
#define FOO_TYPE_BAR (foo_bar_get_type ())
G_DECLARE_FINAL_TYPE (FooBar, foo_bar, FOO, BAR, GObject)
G_END_DECLS
#endif /* FOO_BAR */
foo-bar.c
#include "foo-bar.h"
struct _FooBar
{
GObject parent_instance;
};
G_DEFINE_TYPE (FooBar, foo_bar, G_TYPE_OBJECT);
static void foo_bar_class_init (FooBarClass *klass)
{
}
static void foo_bar_init (FooBar *app)
{
}
制作文件
foo-bar.o: foo-bar.c
gcc -c foo-bar.c $(shell pkg-config --cflags gobject-2.0) -o foo-bar.o
# included to debug the macro
foo-bar.pp: foo-bar.c
gcc -E foo-bar.c $(shell pkg-config --cflags gobject-2.0) -o foo-bar.pp
版本
- glib 2.66.7
- GCC 10.2.1
- Linux (Fedora 33)
我尝试过的
我研究过glib's documentation on how to create a new class,据我所知,我做得对。我还查看了令牌粘贴和宏扩展的工作原理,并尝试在谷歌上搜索我的错误。这一定很明显,但我不知道是什么。我还尝试使用私有数据声明一个可派生类,但是在找到一个最小示例时,我将其更改为没有私有数据的最终类,但仍然有相同的错误。我读到了关于## 运算符here 的信息,但仍然无法判断出了什么问题。
相关堆栈溢出问题
最后,我查看了以下相关的 stackoverflow 帖子:
我的错误是预处理器没有发出连接的令牌。在this 帖子中,用户定义了自己的宏,并询问为什么一个调用有效而另一个无效。我使用的是库而不是我自己的宏。
This 问题是询问为什么宏在 unicode 模式和 ascii 模式下的扩展方式不同。我不是在处理 unicode,所以它不相关。
我没有使用 C++,只有没有泛型语法的 C,所以 this 的问题不适用于我。
由于我没有使用 XCode this 问题并没有解决我的问题。
我了解宏扩展应该如何工作,所以this 问题确实告诉了我任何我不知道的事情。
我没有定义任何新的宏,也没有命名空间冲突的问题,不像this 问题。
在this 和this 问题中,用户不知道宏中的连接运算符。我是,我的问题是它也没有按我期望的方式工作。所以说运营商存在对我没有帮助。
This question is close to my problem,但它的发生是因为没有提供宏的参数。我提供了所有论据,所以它对我没有帮助。
This question the user is using the pasting operator when they don't need to.。在我的情况下,粘贴运算符是必需的。
【问题讨论】:
标签: c compiler-errors macros c-preprocessor gobject