多数の *.c ファイルにわたって構造体の多数のインスタンスを定義 (および初期化) したいのですが、コンパイル時にそれらを 1 つの連続した配列にまとめたいと考えています。カスタムセクションの使用と、セクションの開始アドレスと終了アドレスを構造体の配列の開始と終了として使用することを検討していますが、詳細はまだよくわかっていません。私がそれを回避できる場合は、リンカースクリプト。これは、うまくいかなかった私の最初のハックの要約です。
// mystruct.h:
typedef struct { int a; int b; } mystruct;
// mycode1.c:
#include "mystruct.h"
mystruct instance1 = { 1, 2 } __attribute__((section(".mysection")));
// mycode2.c:
#include "mystruct.h"
mystruct instance2 = { 3, 4 } __attribute__((section(".mysection")));
// mystruct.c:
extern char __mysection_start;
extern char __mysection_end;
void myfunc(void) {
mystruct * p = &__mysection_start;
for ( ; p < &__mysection_end ; p++) {
// do stuff using p->a and p->b
}
}