C : Warning about visibility of a struct

走远了吗. 提交于 2020-01-05 08:24:22

问题


I have a complex C project. In a file message.h I declare this structure

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;                             
};

In the file "castfunctions.h", I include the file "message.h" and I declare the function "setClientNat"

#include <message.h>
void *setClientNat(struct message *msg);

When I compile, I have this warning

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

Can anyone help me?


回答1:


declaration of 'struct message' will not be visible outside of this function [-Wvisibility]

That warning means that struct message was not declared at that point, so it serves as a useless forward declaration.

This means that the code you show is not the complete truth, your files have a lot more in them that what you show - the error is in the code not shown to us.

Here is a few reasons as to why you might get the warning;

  • #include <message.h> includes an entierly different file than what you think it does, go look for another message.h elsewhere.

  • You have include guards in your message.h like so

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

Then you use the headerfiles in a source file like so:

   #include <thisnthat.h>
   #include <message.h>

And it just so happened that the <thisnthat.h> file also defined a
MESSAGE_H macro, rendering the entire message.h invisible. Alternatively the thisnthat.h header have a #define message something_else

  • There's a syntax error somewhere in the header files directly or indirectly included together with message.h. Go hunt for missing ; or { or }

  • You misspelled something. Your comment states the error is gone when you did a typedef struct Message which for some reason have Message with a capital M. So somewhere you're mixing up struct Message vs struct message




回答2:


In addition to nos' answer you should run gcc with the -E option instead of -c. This will output the preprocessed translation unit, so you can see what the compiler really sees. The output also mentions each file that gets included.



来源:https://stackoverflow.com/questions/20007364/c-warning-about-visibility-of-a-struct

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!