AVR / Linux GCC C++ 的常用常量项目
我正在为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以继续当前使用
const char* const
的想法(如果std::string
不可用)。我建议仅将#define
用于分配目的。例子:You can continue with the current idea of using
const char* const
(ifstd::string
is not usable). I would suggest to use#define
for assignment purpose only. Example:我认为你实际上混淆了事情:
如果您想将程序存储器(= AVR 闪存)用于常量字符串,请使用 avr/pgmspace.h 中的 PSTR 宏。
I think that you're in fact confusing things:
If you want to use program memory (= AVR flash) for constant strings, use the PSTR macro from avr/pgmspace.h.