2

I have a variable that is a struct, defined in a .c file:

struct {
    int write_cursor;
    int read_cursor;
    message messages[10];
} out_messages = {0, 0};

To make it available in other files I have an .h file with:

extern struct {
    int write_cursor;
    int read_cursor;
    message messages[10];
} out_messages;

This worked with the Microchip C18 compiler. The XC8 compiler gives an error:

communications.c:24: error: type redeclared
communications.c:24: error: conflicting declarations for variable "out_messages" (communications.h:50)
AndreKR
  • 32,613
  • 18
  • 106
  • 168

2 Answers2

3

The notation isn't correct, you can do:

typedef struct {
    int write_cursor;
    int read_cursor;
    message messages[10];
} Struct_out_messages;

extern Struct_out_messages out_messages;

and in a .c make the initialization.

Struct_out_messages out_messages = {0, 0, {0}};

This compiles in XC16 without any problem, hope it does also on XC8.

user1368116
  • 121
  • 6
  • Strangely, no (the latter). It gives me `communications.c:18: error: no identifier in declaration`. I wonder if I'm missing something else. – AndreKR Mar 11 '13 at 21:35
2

If you need to access the contents of the struct in multiple files, you could change the declaration in the header file to:

struct out_messages_t {
    int write_cursor;
    int read_cursor;
    message messages[10];
};

extern struct out_messages_t out_messages;

and then in your .c file, define and initialise:

struct out_messages_t out_messages = {0, 0};

(and include the header)

teppic
  • 8,039
  • 2
  • 24
  • 37