我試圖在linux內核中學習更多關於kobject的知識,並且在嘗試編寫使用這種設施的模塊時,我收到錯誤和警告消息,所以我已經在這裏放下了相關數據結構的修剪版本以及相應的gcc的錯誤和警告信息。指定的初始化程序:GCC警告消息:臨近初始化和其他警告消息
$ gcc issue.c
issue.c:30:1: error: initializer element is not constant
} ;
^
issue.c:30:1: error: (near initialization for ‘first.attr’)
issue.c:34:1: error: initializer element is not constant
};
^
issue.c:34:1: error: (near initialization for ‘second.attr’)
issue.c:39:8: warning: initialization from incompatible pointer type [enabled by default]
struct bin_attribute *my_bin_attrs[] = { &first_bin_attrs, &second_bin_attrs };
^
issue.c:39:8: warning: (near initialization for ‘my_bin_attrs[0]’) [enabled by default]
issue.c:39:8: warning: initialization from incompatible pointer type [enabled by default]
issue.c:39:8: warning: (near initialization for ‘my_bin_attrs[1]’) [enabled by default]
和樣例代碼:
#include <stdio.h>
struct attribute {
const char *name;
unsigned short mode;
};
struct bin_attribute {
struct attribute attr;
unsigned int size;
void *private;
};
struct attribute_group {
const char *name;
struct attribute **attrs;
struct bin_attribute **bin_attrs;
};
struct attribute first_attr = {
.name = "FIRST"
};
struct attribute second_attr = {
.name = "SECOND"
};
struct bin_attribute first = {
.attr = first_attr
} ;
struct bin_attribute second = {
.attr = second_attr
};
struct bin_attribute *first_bin_attrs = &first;
struct bin_attribute *second_bin_attrs = &second;
struct bin_attribute *my_bin_attrs[] = { &first_bin_attrs, &second_bin_attrs };
int main(void)
{
struct attribute_group my_group = {
.name = "xyz",
.bin_attrs = my_bin_attrs,
};
return 0;
}
[嘗試使用const初始化變量時,錯誤「initializer元素不是常量」的可能重複(http://stackoverflow.com/questions/3025050/error-initializer-element-is-not-constant-when-試圖初始化變量-W) – Dukeling
當你收到一條錯誤信息時,你應該** !!總是!! **谷歌錯誤消息試圖找出它,然後再問一個關於它的問題。 – Dukeling
「和其他警告信息」當然不是理想的問題。一個問題應該是關於一個錯誤(/問題)(並且它們是錯誤的,而不是警告 - 這是有區別的)。 [so]上的帖子旨在爲許多用戶提供長期價值。如果你通過詢問許多事情來污染帖子,任何人在尋找特定帖子時遇到這個帖子的機會都很渺茫,如果他們這樣做了,他們將不得不篩選所有其他不適用的信息到其他錯誤。 – Dukeling