AVR / Linux GCC C++ 的常用常量项目

发布于 2024-12-03 11:05:01 字数 1018 浏览 3 评论 0原文

我正在为 Linux + AVR Arduino 项目创建软件。显然,整个工作分为 Eclipse 中的几个项目(我没有使用 Arduino IDE)。我想为所有这些项目使用常见的(主要是字符串)常量。我还必须保留微控制器的 RAM,以便需要编译时常量。我如何最好地实施它?我的想法是为这些常量创建一个单独的、仅包含标题的项目。

使用:

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

还不够好,因为我希望能够像这样连接字符串常量:

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

我也不想使用define。我想使用:

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

它允许字符串连接并且是(据我所知)编译时,但是 std::string 在 avr 项目中不可用 - 或者我在这里错了,只是不知道如何使用它。

任何帮助表示赞赏。

I'm creating software for an Linux + AVR Arduino project. Obviously the whole work is split in several projects in Eclipse (I'm not using Arduino IDE). I'd like to use common, mostly string, constants for all those projects. I also have to spare microcontroller's RAM so compile-time constants needed. How do I best implement it? My idea is to create a separate, header-only, project for those constants.

Using:

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

is not good enough, because I'd like to be able to concatenate string constants like this:

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 don't want to use define. I'd like to use:

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

which allows for string concatenation and is (AFAIK) compile-time, 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 appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静待花开 2024-12-10 11:05:01

您可以继续当前使用 const char* const 的想法(如果 std::string 不可用)。我建议仅将 #define 用于分配目的。例子:

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

You can continue with the current idea of using const char* const (if std::string is not usable). I would suggest to use #define for assignment purpose 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
风筝有风,海豚有海 2024-12-10 11:05:01

对我来说,编译时间意味着它存储在 ROM(例如微控制器的闪存)中,而运行时变量存储在 RAM 中。就我而言,我必须腾出 RAM。编译器根据许多规则决定将变量放在哪里。定义是编译时常量的示例之一,它们显然不计入 RAM 使用量。另一个例子应该是静态类常量 - 但在这种情况下,我的编译器会做出不同的决定。当然,我可能会混淆一些事情。

我认为你实际上混淆了事情:

  • 定义不存储在 ROM 中 - 它们根本不是你的程序的一部分,因为它们已经由预处理器评估了。
  • 您所做的“编译时”和“运行时”之间的差异适用于评估,而不是存储。

如果您想将程序存储器(= AVR 闪存)用于常量字符串,请使用 avr/pgmspace.h 中的 PSTR 宏。

Compile time for me means it is stored in ROM (eg. microcontroller's flash), where as runtime variables are stored in RAM. In my case, it's RAM I have to spare. The compiler decides where to put variables basing on many rules. Defines are one example of compile-time constants, they clearly don't count up to RAM usage. An other example should be static class constants - but in this case my compiler decides otherwise. Of course, I may be confusing things.

I think that you're in fact confusing things:

  • defines are not stored in ROM - they are not part of your program at all as they get evaluated by the preprocessor already.
  • the difference between "compile time" and "run time" you're making applies to evaluation, not storage.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文