struct option出错:数组类型的元素类型不完整

时间:2014-04-30 07:22:14

标签: c arrays getopt-long

我尝试构建一个解析命令行的函数。但是,当我定义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'}
        };
       }
}

你能帮我解释为什么我会收到这些错误吗?

1 个答案:

答案 0 :(得分:3)

确保你这样做:

#include <getopt.h>

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