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

Eng*_*eaR 1 c c++ gcc clang

我有一个复杂的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;                             
};
Run Code Online (Sandbox Code Playgroud)

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

#include <message.h>
void *setClientNat(struct message *msg);
Run Code Online (Sandbox Code Playgroud)

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

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

谁能帮我?

nos*_*nos 6

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

那个警告意味着那时struct message没有宣布,所以它是一个无用的前方声明.

这意味着您显示的代码并非完整的事实,您的文件中包含的内容与您显示的内容相同 - 错误在代码中未显示给我们.

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

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

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

#ifndef MESSAGE_H
#define MESSAGE_H 
struct message { 
....
};
#endif`
Run Code Online (Sandbox Code Playgroud)

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

   #include <thisnthat.h>
   #include <message.h>
Run Code Online (Sandbox Code Playgroud)

事实上,该<thisnthat.h>文件还定义了一个
MESSAGE_H宏,使整个message.h不可见.或者thisnthat.h标题有一个#define message something_else

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

  • 你错了一些东西.您的评论指出,当您typedef struct Message 因某种原因Message使用资金时,错误消失了M.所以,地方你混淆struct MessageVSstruct message