如何将变量放置在内存中的给定绝对地址处(使用 GCC) [英] How to place a variable at a given absolute address in memory (with GCC)

查看:57
本文介绍了如何将变量放置在内存中的给定绝对地址处(使用 GCC)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

RealView ARM C 编译器支持在给定的内存地址放置一个变量使用变量属性 at(address):

The RealView ARM C Compiler supports placing a variable at a given memory address using the variable attribute at(address):

int var __attribute__((at(0x40001000)));
var = 4;   // changes the memory located at 0x40001000

GCC 是否有类似的变量属性?

Does GCC have a similar variable attribute?

推荐答案

我不知道,但您可以轻松创建如下解决方法:

I don't know, but you can easily create a workaround like this:

int *var = (int*)0x40001000;
*var = 4;

完全不是一回事,但在大多数情况下是完美的替代品.它适用于任何编译器,而不仅仅是 GCC.

It's not exactly the same thing, but in most situations a perfect substitute. It will work with any compiler, not just GCC.

如果您使用 GCC,我假设您也使用 GNU ld(当然,虽然这不确定)并且 ld 支持放置变量任何地方你想要它们.

If you use GCC, I assume you also use GNU ld (although it is not a certainty, of course) and ld has support for placing variables wherever you want them.

我认为让链接器完成这项工作很常见.

I imagine letting the linker do that job is pretty common.

受到@rib 回答的启发,我会补充一点,如果绝对地址用于某个控制寄存器,我会将 volatile 添加到指针定义中.如果只是内存,没关系.

Inspired by answer by @rib, I'll add that if the absolute address is for some control register, I'd add volatile to the pointer definition. If it is just RAM, it doesn't matter.

这篇关于如何将变量放置在内存中的给定绝对地址处(使用 GCC)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆