I have the following static array in header file:
static MyStruct_t MyStructArray[] = {
......
......
......
}
But gcc issues a warning:
warning: `MyStructArray' defined but not used
What is the correct way to handle the situation?
UPD:
Defining the array as const:
const MyStruct_t MyStructArray[] = {
......
fixes thwe situation. So what is the preferred way extern or const in header?
Because you’ve declared the array static in a header file, each compilation unit (i.e. preprocessed .cpp file) gets its own copy of the array–almost certainly not what you intended, and the sure reason that you get the “defined but not used” error.
Instead, you probably want this in your header file:
…and then in exactly 1 .cpp file: