gpt4 book ai didi

c++ - 我怎样才能让 GCC 在 ROM 中放置一个 C++ constexpr?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:46:24 26 4
gpt4 key购买 nike

我为 LPC1114 编译,这是一个小型 ARM(实际上是 Cortex)目标。 RAM 比 ROM 更受限制。我使用最新的 Mentor (CodeBenchLite) GCC 编译器 (GCC 4.6.3)。我有一些我想在 ROM 中拥有的常量对象。据我了解,下面代码中的 ffx 对象应该在 ROM(代码)中结束,但它被放置在 DATA 中。

class flop {
public:
int x;
constexpr flop( int x ) : x(x){}
};

extern constexpr flop ffx( 1 );

如何说服编译器预先计算对象并将其放入 ROM?

或者我应该问:

  • 我能以某种方式期望 G++ 编译器为 ffx 生成 ROMable 数据吗
  • 如果是这样,我的代码是否正确
  • 如果是这样,支持哪个 G++ 版本(我使用 4.6,也许我需要 4.7?)

=======================================

这个 bugzilla 条目 c++/49673似乎表明我的是一个已知问题,可能已在 GCC 4.7 中修复。不幸的是,我更喜欢使用内置的 Mentor/CodeSourcery,它仍然是 4.6.3。所以我想我暂时被这个错误困住了。 :(

最佳答案

Update #2: Test results with gcc 4.7.0

这确实在 gcc 4.7.0 中得到了修复。您的代码产生以下汇编器输出:

    .file   "constexpr.cpp"
.globl _ffx
.section .rdata,"dr"
.align 4
_ffx:
.long 1

这可能是您想要的。升级?

我认为为此您需要一个特定于平台的解决方案。参见 Locating code and data in memory (Scatterloading) :

Scatterloading allows you to partition an application into a number of separate code and data regions spread throughout the address map. The location of these regions can differ between load time and run time:

  1. Load Regions contain application code and/or data used by the application at power-on/load time (typically ROM).

与其单独依赖 gcc,不如尝试使用上面提供的链接中建议的 armlink

更新:我看到了您提到的 gcc 错误。 LLVM/MinGW 同样无望。不过,我一直在研究 GCC 4.6,这里有一些我认为可能对您有帮助的东西:

struct Data
{
int i;
};
constexpr Data getData() { return Data{1}; }

如果您没有构造函数,而是有一个 constexpr 函数,并尝试生成汇编器输出,则输出通常为空(没有 .data 部分) .如果您使用它来初始化一些变量(在全局范围内),如下所示:

const Data foo = getData();

你会得到如下的程序集:

    .file   "constexpr.cpp"
.section .rdata,"dr"
.align 4
__ZL3foo:
.long 1

(这是使用 g++ -std=c++0x -S 命令行)。这对你有用吗?

关于c++ - 我怎样才能让 GCC 在 ROM 中放置一个 C++ constexpr?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10982249/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com