I have a header file where I would like to define a few default colors.
typedef struct _color {
unsigned char r;
unsigned char g;
unsigned char b;
unsigned char a;
} color;
/* Default colors */
// initializes to (0, 0, 0, 0)
color TRANSPARENT;
color color_black;
color_black.a = 255;
color color_red;
color_red.r = 255;
color_red.a = 255;
color GREEN;
GREEN.g = 255;
GREEN.a = 255;
When I compile with clang -I. -c -o image.o image.c, I get the following errors:
In file included from image.c:5:
./image.h:25:1: error: unknown type name 'color_black'
color_black.a = 255;
^
./image.h:25:12: error: expected identifier or '('
color_black.a = 255;
^
./image.h:28:1: error: unknown type name 'color_red'
color_red.r = 255;
^
./image.h:28:10: error: expected identifier or '('
color_red.r = 255;
...
This continues for all of the colors I have defined.
I'm not sure why it thinks the variables should be type names, especially when I have already declared them. I have tried using extern and changing the variable names' style to ALL CAPS.
color_black.a = 255;You can't write statement at global scope, all such code must go inside a function. If you just want to initialize the variables, writecolor color_black = { 0, 0, 0, 255 };instead.struct color { ... };