4

我尝试构建一个解析 cmd 行的函数。但是,当我定义long_options数组时,会出现编译错误:

error: array type has incomplete element type
error: field name not in record or union initializer
error: (near initialization for 'long_options')
// and so on for every defined line in the 'long_options' 

编码:

//parse_cmd.c
void parse_cmd(int argc, char *argv[]) {
    while (1) {
        int input_char;
        static struct option long_options[] = {
                {.name = "dev-name", .has_arg = 1, .val = 'd'},
                {.name = "tcp-port", .has_arg = 1, .val = 't'},
                {.name = "ib-port",  .has_arg = 1, .val = 'i'},
                {.name = "seed",     .has_arg = 1, .val = 's'},
                {.name = "iters",    .has_arg = 1, .val = 'I'},
                {.name = "mask",     .has_arg = 1, .val = 'm'},
                {.name = NULL,       .has_arg = 0, .val = '\0'}
        };
       }
}

你能帮助我为什么会得到这些错误吗?

4

1 回答 1

5

确保你这样做:

#include <getopt.h>

在 C 文件的开头,拉入getopt()函数 prototyp 及其相关声明,包括struct option.

于 2014-04-30T07:24:00.667 回答