我想定義(並初始化)一些* .c文件中結構體的實例,但我希望它們收集在編譯時成爲一個連續的數組。我一直在研究使用一個自定義的部分,並使用該部分的開始和結束地址作爲結構數組的開始和結束,但我還沒有完全弄清楚細節,我寧願不寫一個自定義鏈接器腳本,如果我可以擺脫它。下面是其中沒有相當的工作我的第一次入侵的總結:在編譯時將多個文件中的變量收集到一個連續的內存塊中
// 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
}
}
我現在用的是XC16工具鏈由Microchip。 –