Win10
TDM-GCC 4.9.2 64bit
只知道extern的粗淺概念,有錯糾正
如果a檔案的一個變數前面前置字有extern 那該變數就是在其他檔案有定義,在link階段才
會把extern變數的位址提供給a(是這樣說嗎?),編譯時a只會知道這個變數的名稱跟型態而
已
範例:
https://goo.gl/rBdTXw 我把const前置詞先拿掉 先不考慮const的問題
file1.c:
unsigned char data1[] =
{
0x65, 0xF0, 0xA8, 0x5F, 0x5F,
0x5F, 0x5F, 0x31, 0x32, 0x2E,
0x31, 0xF1, 0x63, 0x4D, 0x43,
0x52, 0x45, 0x41, 0x54, 0x45,
0x44, 0x20, 0x42, 0x59, 0x3A,
0x20, 0x69, 0x73, 0x70, 0x56,
};
unsigned int data1_size = sizeof(data1) / sizeof(data1[0]);
unsigned int data1_version = 1;
main.c:
struct Data_Info
{
unsigned char * data_ptr;
unsigned int data_size;
unsigned int data_version;
};
extern unsigned char data1[];
extern unsigned int data1_size;
extern unsigned int data1_version;
static struct Data_Info Data_Info_Table[] =
{
{ data1, data1_size, data1_version},
};
int main(void)
{
return 0;
}
[Error] initializer element is not constant
[Error] (near initialization for 'Data_Info_Table[0].data_size')
[Error] (near initialization for 'Data_Info_Table[0].data_version')
下面的回應是說The problem is that the compiler doesn't know the value to place
into the data_size and data_version members of the field.
ok,但是我不太確定結構的第一個成員指標變數為什麼沒有問題?
是因為它是位址? 在compiler階段會跟編譯器講data1有定義在某處,所以link階段就會得
link到? 是不是跟引用外部function的概念一樣?
謝謝