Large table declaration in the C ++ header

advertisements

It's been ages since I last worked with C++.

Situation: I have a large array that needs to be available in different .cpp files. It is immutable, so I thought I better put a const char array[] = … in the header file. But now the array appears several times in the compiled binary, as far as I can see.

What is the proper way to declare large constant arrays in a header, so they won't be compiled into every source object?


If you define an array in a header file (whether or not you initialise it), you will get lots of copies.

You want to declare it in the header:

extern const char array[];

and define it like this in your .c / .cpp file:

const char array[] = ... ;