【问题标题】:Allocate an array in C a specific location using linker commands使用链接器命令将 C 中的数组分配到特定位置
【发布时间】:2017-04-14 13:22:23
【问题描述】:

我是初学者...我想写入嵌入式闪存中的特定内存位置...如何在我的 C 头文件中提及它?然后使用链接器脚本将它与特定的内存位置链接起来。现在我已经将数组声明为 extern 并且它可以正确编译。在喜欢的同时,我需要告诉链接器我在这个特定位置需要它。是否应该在 .ld 文件中给出?什么是 .dld 文件?这不适用于 GCC,不适用于 diab 编译器。我已经看到了冒泡排序的示例代码bubble.dld。但是在某些项目中,在制作项目时会创建 .dld 文件。它实际上是在哪一步创建的?

【问题讨论】:

  • 什么?你有内存映射的闪存驱动器吗?这听起来很不寻常……这是什么硬件平台?
  • 是的,我确实有一个内存映射闪存。它是一个 ST 微控制器...
  • @unwind 或者帮我把它写到一个普通的内存位置......
  • 微控制器上的嵌入式闪存通常不被称为“闪存驱动器”,这让我感到困惑。此外,它并不总是易于写入,即您不能总是期望只将数据移动到那里,有时您需要做其他事情(要戳的外围寄存器)以控制写入过程。您需要准确指定您正在谈论的控制器,才能使这个问题甚至可以远程回答。
  • SPC56EC64 是使用的微控制器。感谢您的建议。我已经编辑了它。

标签: c linker linker-errors ld extern


【解决方案1】:

第一个解决方案

在“.c”中:

// Talk to linker to place this in ".mysection"
__attribute__((section(".mysection"))) char MyArrray[52];

在“.ld”中:

MEMORY {
   m_interrupts (RX)    : ORIGIN = 0x00040000, LENGTH = 0x000001E8
   m_text      (RX)     : ORIGIN = 0x00050000, LENGTH = 0x000BFE18
   /* memory which will contain secion ".mysection" */
   m_my_memory (RX)       : ORIGIN = 0x00045000, LENGTH = 0x00000100
}

SECTIONS
{
  /***** Other sections *****/

  /* place "mysection" inside "m_my_memory" */
  .mysection :
  {
    . = ALIGN(4);
    KEEP(*(.mysection));
    . = ALIGN(4);
  } > m_my_memory

  /***** Other sections *****/
}

第二种解决方案

在“.c”中

extern char myArray[52];

在“.ld”中

MEMORY {
   m_interrupts (RX)    : ORIGIN = 0x00040000, LENGTH = 0x000001E8
   m_text      (RX)     : ORIGIN = 0x00050000, LENGTH = 0x000BFE18
   /* memory which will contain secion "myArray" */
   m_my_memory (RX)       : ORIGIN = 0x00045000, LENGTH = 0x00000100
}

SECTIONS
{
  /***** Other sections *****/

  /* place "myArray" inside "m_my_memory" */
  .mysection :
  {
    . = ALIGN(4);
    myArray = .; /* Place myArray at current address, ie first address of m_my_memory */
    . = ALIGN(4);
  } > m_my_memory

  /***** Other sections *****/
}

请参阅this good manual 了解更多如何将元素放置在您想要的位置

【讨论】:

  • 非常感谢 :) 。我的链接器有一个 exe 文件。也就是说,我正在 Windows 环境中构建它。我应该使用新的 ld 文件引用重建 exe 吗?
  • 您不必重建链接器,只需重建最终应用程序
猜你喜欢
  • 2014-07-14
  • 1970-01-01
  • 2021-11-25
  • 1970-01-01
  • 1970-01-01
  • 2020-09-12
  • 1970-01-01
  • 1970-01-01
  • 2019-04-29
相关资源
最近更新 更多