C:有关结构可见性的警告 [英] C : Warning about visibility of a struct

查看:284
本文介绍了C:有关结构可见性的警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

有人可以帮助我吗?

推荐答案


在此
函数[-Wvisibility]

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

之外看不到结构消息的声明

该警告表示此时尚未声明 struct消息,因此它用作无用的前向声明。

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> 包含的文件与您认为的完全不同,请在其他位置查找另一个message.h。

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

您的message.h中包含了警卫。

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>

而恰巧是< thisnthat.h> 文件还定义了一个

MESSAGE_H宏,使整个message.h不可见。
另外, thisnthat.h 头具有 #define消息something_else

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


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

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

您拼写错误。您的评论指出,当您执行 typedef struct Message 时,该错误已消失,由于某种原因,该消息中的 Message 用大写字母 M 。所以在某个地方您混淆了 struct消息 struct消息

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

这篇关于C:有关结构可见性的警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆