Common constants for the AVC / Linux GCC C ++ project

I am creating software for the Linux + AVR Arduino project. Obviously, all the work is divided into several projects in Eclipse (I do not use the Arduino IDE). I would like to use common, mostly string, constants for all these projects. I also have to save the microcontroller from RAM, so compilation constants are needed. What is the best way to implement this? My idea is to create a separate, header-only project for these constants.

Using:

class A { public: static const char * const STRING; static const unsigned char BOOL; }; 

not good enough because I would like to concatenate string constants as follows:

 class A { public: static const char * const STRING_PART1; static const char * const STRING_PART2; static const unsigned char BOOL; }; const char * const A::STRING_PART1 = "PART1_"; //const char * const A::STRING_PART2 = A::STRING_PART1 + "PART2"; //obviously won't compile //const char * const A::STRING_PART2 = strcat("PART2", A::STRING_PART1); //this is not compile-time 

I also do not want to use define . I would like to use:

 class A { public: static const std::string STRING_PART1; static const std::string STRING_PART2; } 

which allows string concatenation and is (AFAIK) compilation, but std :: string is not available in avr projects - or I'm wrong here and just don’t know how to use it.

Any help was appreciated.

+4
source share
2 answers

You can continue to use the idea of const char* const (if std::string not used). I would suggest using #define for assignment only. Example:

 class A { public: static const char * const STRING_PART1; static const char * const STRING_PART2; static const unsigned char BOOL; }; #define PART1_ "PART1_" // <--- for value assignent #define PART2_ "PART2_" const char * const A::STRING_PART1 = PART1_; const char * const A::STRING_PART2 = PART1_ PART2_; // <--- ok! concatenation by compiler 
+3
source

Compilation time for me means that it is stored in ROM (for example, a microcontroller flash), where when run-time variables are stored in RAM. In my case, this is RAM, which I have to save. The compiler decides where to set the variables based on many rules. Definitions are one example of compile-time constants; they do not explicitly take into account the use of RAM. Another example should be the static constants of a class, but in this case my compiler decides otherwise. Of course I can be confusing.

I think you are actually confusing things:

  • are not stored in ROM - they are not part of your program at all, since they are already evaluated by the preprocessor.
  • The difference between the "compilation" time and the "runtime" that you make relates to the evaluation, not the repository.

If you want to use program memory (= AVR flash) for constant lines, use the PSTR macro from avr / pgmspace.h.

+1
source

All Articles