c++assemblyavrprogmem

How to prevent strings from beeing multiple times in PROGMEM


I am trying to safe some program space by trying to reduce the count strings in progmem. So i started to put all stings which are needed multiple times into a file with a namespace:

#include <avr/pgmspace.h>
#ifndef __PSTRINGS__
#define __PSTRINGS__
namespace p_strings
{
//golbal
const char right_arrow[] PROGMEM = ">";
const char procent[] PROGMEM = "%";
const char start[] PROGMEM = "Start";
const char speed[] PROGMEM = "Speed: ";
//... more here
}
#endif

I am using the strings by including them into the file where i need them and for example call Display::out_p(1, 0) << p_strings::right_arrow;. Which reads the byte out of the progmem and put them at the right position of the display. But if i look into the .Iss i see that they are still included multiple times into the ASM.

000000a8 <_ZN9p_stringsL5startE>:
      a8:   53 74 61 72 74 00                                   Start.
...
000000de <_ZN9p_stringsL5startE>:
      de:   53 74 61 72 74 00                                   Start.

So i guess this isnt the right solution to safe storage.

My questions now are:

  1. Why does it still included (in the asm) multiple times? (Maybe some preaty simple failur of my thinking)
  2. How to i create "something" which prevents this? (Do i need somehow a class which i pass as reference?)

If more information is needed let me know.


Solution

  • In C++, a const variable has internal linkage by default (as if it was static)—which normally means that each translation unit (each .cpp file) gets its own copy. This may very well be why you're seeing them multiple times.

    What you'd want to do is declare the variables as extern, and then define them in exactly one source file.