功能之外,为何不显示那些被隐藏的声明?

2026-04-16 19:105阅读0评论SEO资源
  • 内容介绍
  • 相关推荐

本文共计593个文字,预计阅读时间需要3分钟。

功能之外,为何不显示那些被隐藏的声明?

首先,我用Google搜索了错误信息并阅读了相关解答:关于编译器警告关于结构体可见性的问题。以下是一些可能的原因和建议:

1. 结构体成员未声明为public:确保所有需要从外部访问的结构体成员都被声明为public。

2.结构体在文件中未正确声明:如果结构体是在头文件中声明的,确保在实现文件中包含了该头文件。

3.初始化问题:如果结构体成员需要初始化,确保在声明时进行了正确的初始化。

4.作用域问题:确保结构体成员在正确的函数或作用域内被访问。

具体问题可能如下:

- 结构体成员未声明为public。

- 结构体未在文件中正确声明。- 结构体成员未正确初始化。- 结构体成员访问超出作用域。

首先,我用Google搜索了错误并阅读了这些答案:

> I don’t understand why compiler is giving me error with this code
> C : Warning about visibility of a struct

但他们都没有帮助我,所以我们在这里.

问题存在于这两个结构之间,prx_data_s存储通用数据,prx_ops_s定义指向将使用该数据的函数的指针.

我将简化示例的来源:

prx_data.h

#ifndef PRX_EXAMPLE_DATA_H #define PRX_EXAMPLE_DATA_H #include "prx_ops.h" struct prx_data_s { enum prx_op_t op; char *keyquery; }; char *get_query(struct prx_data_s *dt); #endif

prx_data.c

#include "prx_data.h" char *get_query(struct prx_data_s *dt) { return dt->keyquery; }

prx_ops.h

#ifndef PRX_EXAMPLE_OPS_H #define PRX_EXAMPLE_OPS_H #include "prx_data.h" enum prx_op_t { PRX_EXAMPLE_OP = 2 }; struct prx_ops_s { int (*dec) (struct prx_data_s *); }; #endif

我正在尝试使用以下示例编译上述示例中的对象:

功能之外,为何不显示那些被隐藏的声明?

clang -c prx_data.c -o prx_data.o -std=c11 -g -Wall

这是输出错误:

In file included from prx_data.c:1: In file included from ./prx_data.h:4: ./prx_ops.h:11:24: warning: declaration of 'struct prx_data_s' will not be visible outside of this function [-Wvisibility] int (*dec) (struct prx_data_s *); ^

欢迎所有帮助,谢谢:)

您的标头中存在循环依赖项问题:

prx_data.h: #include "prx_ops.h" <<< Here we do not yet see the struct definition prx_ops.h: #include "prx_data.h" <<<< Nothing will be included due to inclusion guards. struct prx_ops_s { int (*dec) (struct prx_data_s *); <<<< Here a new struct type is declared. }; later back in prx_data.h: struct prx_data_s { enum prx_op_t op; char *keyquery; };

本文共计593个文字,预计阅读时间需要3分钟。

功能之外,为何不显示那些被隐藏的声明?

首先,我用Google搜索了错误信息并阅读了相关解答:关于编译器警告关于结构体可见性的问题。以下是一些可能的原因和建议:

1. 结构体成员未声明为public:确保所有需要从外部访问的结构体成员都被声明为public。

2.结构体在文件中未正确声明:如果结构体是在头文件中声明的,确保在实现文件中包含了该头文件。

3.初始化问题:如果结构体成员需要初始化,确保在声明时进行了正确的初始化。

4.作用域问题:确保结构体成员在正确的函数或作用域内被访问。

具体问题可能如下:

- 结构体成员未声明为public。

- 结构体未在文件中正确声明。- 结构体成员未正确初始化。- 结构体成员访问超出作用域。

首先,我用Google搜索了错误并阅读了这些答案:

> I don’t understand why compiler is giving me error with this code
> C : Warning about visibility of a struct

但他们都没有帮助我,所以我们在这里.

问题存在于这两个结构之间,prx_data_s存储通用数据,prx_ops_s定义指向将使用该数据的函数的指针.

我将简化示例的来源:

prx_data.h

#ifndef PRX_EXAMPLE_DATA_H #define PRX_EXAMPLE_DATA_H #include "prx_ops.h" struct prx_data_s { enum prx_op_t op; char *keyquery; }; char *get_query(struct prx_data_s *dt); #endif

prx_data.c

#include "prx_data.h" char *get_query(struct prx_data_s *dt) { return dt->keyquery; }

prx_ops.h

#ifndef PRX_EXAMPLE_OPS_H #define PRX_EXAMPLE_OPS_H #include "prx_data.h" enum prx_op_t { PRX_EXAMPLE_OP = 2 }; struct prx_ops_s { int (*dec) (struct prx_data_s *); }; #endif

我正在尝试使用以下示例编译上述示例中的对象:

功能之外,为何不显示那些被隐藏的声明?

clang -c prx_data.c -o prx_data.o -std=c11 -g -Wall

这是输出错误:

In file included from prx_data.c:1: In file included from ./prx_data.h:4: ./prx_ops.h:11:24: warning: declaration of 'struct prx_data_s' will not be visible outside of this function [-Wvisibility] int (*dec) (struct prx_data_s *); ^

欢迎所有帮助,谢谢:)

您的标头中存在循环依赖项问题:

prx_data.h: #include "prx_ops.h" <<< Here we do not yet see the struct definition prx_ops.h: #include "prx_data.h" <<<< Nothing will be included due to inclusion guards. struct prx_ops_s { int (*dec) (struct prx_data_s *); <<<< Here a new struct type is declared. }; later back in prx_data.h: struct prx_data_s { enum prx_op_t op; char *keyquery; };