C:关于结构可见性的警告

我有一个复杂的C项目。 在文件message.h我声明了这个结构

 struct message { int err; struct header { char *protocol_version; char *type; long int sequence_number; } header; struct body { int num_tag; char *tag_labels[LEN]; int num_attr_tag[LEN]; char *attr_labels[LEN][LEN]; char *attr_values[LEN][LEN]; char *attr_types[LEN][LEN]; } body; }; 

在文件“castfunctions.h”中,我包含文件“message.h”,我声明函数“setClientNat”

 #include  void *setClientNat(struct message *msg); 

当我编译时,我有这个警告

 castfunctions.h:warning: declaration of 'struct message' will not be visible outside of this function [-Wvisibility] void *setClientNat(struct message *msg); 

谁能帮我?

在此函数之外不会显示’struct message’的声明[-Wvisibility]

该警告意味着此时未声明struct message ,因此它用作无用的前向声明。

这意味着您显示的代码并不完全正确,您的文件中包含的内容与您显示的内容相同 – 错误在代码中未显示给我们。

以下是您可能收到警告的原因。

  • #include 包含一个与你的想法完全不同的文件,去其他地方寻找另一个message.h。

  • 你在message.h中包含了警卫,就像这样

 #ifndef MESSAGE_H #define MESSAGE_H struct message { .... }; #endif` 

然后在源文件中使用头文件,如下所示:

  #include  #include  

事实上, 文件也定义了一个
MESSAGE_H宏,渲染整个message.h不可见。 或者, thisnthat.h头部有一个#define message something_else

  • 头文件中某处的语法错误直接或间接包含在message.h中。 寻找失踪; 或{或}

  • 你错了一些东西。 您的注释表明当您执行typedef struct Message时,错误消失了,该typedef struct Message由于某种原因而具有带大写M Message 。 所以,你正在混合struct Message vs struct message

除了nos’的答案,你应该使用-E选项而不是-c运行gcc。 这将输出预处理的翻译单元,因此您可以看到编译器真正看到的内容。 输出还提到了包含的每个文件。