加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

AVR / Linux GCC C项目的常见常量

发布时间:2020-12-13 23:01:59 所属栏目:Linux 来源:网络整理
导读:我正在为 Linux AVR Arduino项目创建软件.显然,整个工作分为Eclipse中的几个项目(我没有使用Arduino IDE).我想为所有这些项目使用常见的,主要是字符串的常量.我还需要备用微控制器的RAM,因此需要编译时常量.我该如何最好地实现它?我的想法是为这些常量创建
我正在为 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;
}

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

任何帮助赞赏.

解决方法

您可以继续使用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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读