简体   繁体   中英

Error with struct option: array type has incomplete element type

I try to build a function for parsing cmd line. But, when I define the long_options array I get the compile errors:

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' 

The code:

//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'}
        };
       }
}

Can you please help why I get these errors?

Make sure you do:

#include <getopt.h>

in the beginning of the C file, to pull in the getopt() function prototyp and its related declarations, including struct option .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM