I’m using a pair of global variables in one of my .c
files, matched to a single extern
declaration each in two different .h
files (well, one .h
file, preprocessed two ways). One is for public consumption, and one is for private use. Both are const
variables.
I only want to initialize one of the variables in my .c
file, and then assign the second variable to the same content. Here’s the relevant contents of the .c
file at the moment:
struct List_methods const List = {
.create = List__create,
.create_naughty = List__create_naughty,
// …
};
struct List_methods const Paws__List = List;
… and the corresponding .h:
#if defined(EXTERNALIZE)
# define List_methods Paws__List_methods
# define List Paws__List
#endif
// …
struct List_methods {
list (*create) (void);
list (*create_naughty) (void);
// …
} const extern List;
#if defined(EXTERNALIZE)
# undef List_methods Paws__List_methods
# undef List Paws__List
#endif
The goal here, is to ensure that when the .h
is included with EXTERNALIZE
defined, the including file gains access to the Paws__List
variable, extern
’d to the definition in my .c
file. However, if it’s included without that definition, they gain access to an extern
’d List
instead (which I intend to use in my internal files, and make available if the #include
er wants it).
However, the Paws__List = List
assignment blows up in my compiler, with the following error:
Source/Paws.o/list/list.c:32:40: error: initializer element is not a
compile-time constant
struct List_methods const Paws__List = List;
^~~~
I’m looking for any help I can get to make this work as described above (that is, to define two const
names for the same struct in my .c
file, such that one or the other can be referenced by the .h
header.)