首页 > 解决方案 > extern struct forward declaration

问题描述

I am trying to compile some old code that include a mix of C and C++. I found following syntax:

extern struct Edge;
typedef struct Edge
{
    ...
    Edge* edges;
    ...
} Edge;

When I try to compile with GCC I get an error:

a storage class can only be specified for objects and functions extern struct Edge;

After I remove extern it compiles. I might be mistaken, but it does look to me like a forward declaration of Edge, but why there an extern keyword at the front of struct?

标签: c++cgcc

解决方案


在 C 中,是有效的但没有用:它的含义与Inextern struct S;完全相同,适用于, , , ... 无论声明了多少变量,即使声明了零个变量。struct S;extern struct S a, b, c, ...;externabc

在 C++ 中,extern struct S;无效。为了与 C 兼容,大多数 C++ 编译器都允许它作为扩展,但 GCC 不允许。您只是删除了extern关键字是对的。

可能最初编写的代码extern struct S s;被拆分为 and 的单独声明struct Ss并且extern不小心留下了。由于编译器不介意,所以作者没有注意到。


推荐阅读