使用链接器命令将 C 中的数组分配到特定位置

Allocate an array in C a specific location using linker commands

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

第一个解决方案

在“.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 了解更多如何将元素放置在您想要的位置