EngAndreaR
EngAndreaR

Reputation: 31

C : Warning about visibility of a struct

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?

Upvotes: 2

Views: 5709

Answers (2)

Roland Illig
Roland Illig

Reputation: 41686

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.

Upvotes: 1

nos
nos

Reputation: 229284

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

Upvotes: 7

Related Questions