Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning: declaration of '...' will not be visible outside this function [-Wvisibility]

Tags:

c

First of all, I've googled the error and read these answers:

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

But none of them helped me, so here we are.

The problem resides somewhere in between these 2 structures, prx_data_s which store generic data and prx_ops_s that defines pointers to functions that will use that data.

I'll simplify the sources for the example:

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

I'm trying to compile the object from the above example with:

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

And this is the output error:

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

All help is welcome, thanks :)

like image 592
criw Avatar asked Aug 23 '18 12:08

criw


2 Answers

You have a problem with circular dependencies in your header:

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;
};
like image 63
Gerhardh Avatar answered Sep 18 '22 05:09

Gerhardh


When reading prx_ops.h, you dont 't have prx_data.h included because the compiler is reading prx_ops.h from the include at the beginning of prx_data.h. You thus have to forward declare it.

Try adding

struct prx_data_s;

at the beginning of prx_ops.h`

Hope that helps ~~

like image 20
Yanis.F Avatar answered Sep 18 '22 05:09

Yanis.F