2

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?

Haris
  • 12,120
  • 6
  • 43
  • 70
EngAndreaR
  • 31
  • 1
  • 4
  • 3
    What and where is line `castfunctions.h:36`? – alk Nov 15 '13 at 17:47
  • 2
    Did you put the `#include` inside the function body by mistake? – Arkku Nov 15 '13 at 17:48
  • 1
    There is nothing obviously wrong with the structure you show. Can you construct a [sscce](http://sscce.org/) that demonstrates this problem? – simonc Nov 15 '13 at 17:50
  • 1
    Did you declare that `struct` inside a function? – Luchian Grigore Nov 15 '13 at 17:51
  • I insert the declaration of setClientNat at line 36. No I don't put the include inside the function body. – EngAndreaR Nov 15 '13 at 17:51
  • No I declare that struct in the header file 'message.h'. After, I include this file in another header file 'castfunctions.h', where I declare the function 'setClientNat'. – EngAndreaR Nov 15 '13 at 17:55
  • 1
    If you are not deliberately including either header inside a function, my guess is that you are missing a semicolon or `}` somewhere above `#include `, but it is impossible to answer accurately with the information available. – Arkku Nov 15 '13 at 17:59
  • If I declare `typedef struct Message`, I delete this warning. But I cannot do this because this change generate errors related to other file of the project. – EngAndreaR Nov 15 '13 at 18:10
  • `message.h` is a pretty generic-looking name. Are you sure you're getting the right one? Maybe another one is hiding in `/usr/include` –  Nov 15 '13 at 18:24
  • Wait, wait, wait. I can't say with 100% certainty for C++, but if you're compiling this as C, could this be because you have `#include ` and not `#include "message.h"`? – Dennis Meng Nov 15 '13 at 18:42

2 Answers2

7

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

nos
  • 223,662
  • 58
  • 417
  • 506
1

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.

Roland Illig
  • 40,703
  • 10
  • 88
  • 121