【问题标题】:Warning: declaration of '...' will not be visible outside this function [-Wvisibility]警告:“...”的声明在此函数之外不可见 [-Wvisibility]
【发布时间】:2018-08-23 12:21:10
【问题描述】:

首先,我搜索了错误并阅读了以下答案:

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

问题存在于这两个结构之间,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_ops 包含 prx_data,反之亦然。尽管包含守卫打破了任何无限循环,但在您的示例中,首先读取 ops 标头,从而导致结构的前向声明。
  • 哦,我不知道循环依赖是一回事,这是有道理的。谢谢@TypeIA!
  • 一般来说,正如你所发现的,你不应该在头文件中定义函数

标签: c


【解决方案1】:

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

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

【讨论】:

  • 我不知道循环依赖是一回事,这完全有道理。谢谢!我会尽快接受答案
【解决方案2】:

在读取prx_ops.h 时,您没有包含prx_data.h,因为编译器正在从prx_data.h 开头的包含中读取prx_ops.h。因此,您必须转发声明它。

尝试添加

struct prx_data_s;

在prx_ops.h的开头`

希望对你有所帮助~~

【讨论】:

  • 感谢 Yanis,这很有道理,我完全忽略了它,它帮助很大!
猜你喜欢
  • 2011-12-07
  • 2021-06-29
  • 2021-01-14
  • 2022-06-14
  • 2017-11-12
  • 1970-01-01
  • 2022-09-23
  • 1970-01-01
  • 2012-01-16
相关资源
最近更新 更多