opts.h:
#ifndef PINF_OPTS_H
#define PINF_OPTS_H
#endif //PINF_OPTS_H
// == DEFINE ==
#define MAX_OPTS 100
// == VAR ==
struct _opt {
char *option; // e.g. --group
char *alias; // e.g. -G
int reqArg; // Require Argument | 0: No 1: Yes
int maxArgs; // -1: Undefined/ Unlimited
int func; /* Run Function? 0: No 1: Yes
* If No, it can be checked with function 'isOptEnabled'
*/
} opt;
struct _optL {
struct opt avOpt[MAX_OPTS];
} optL;
struct _acOpt {
struct opt *acOpt[MAX_OPTS];
} acOpt;
// == FUNC ==
void initOpts(void);
opts.c:
#include "opts.h"
#include <stdio.h>
#include <stdlib.h>
// == VAR ==
static struct optL *optList;
static struct acOpt *activeOpts;
// == CODE ==
void initOpt(void) {
optList = (struct optL *)malloc(sizeof(struct optL *));
activeOpts = (struct acOpt *)malloc(sizeof(struct acOpt *));
}
opts_test.c:
#include <stdio.h>
#include "../include/opts.h"
int main(void) {
initOpts();
return 0;
}
我用以下代码编译它:
gcc -c include / opts.c&amp;&amp; gcc -c opts_test.c&amp;&amp; gcc -o opts_test opts_test.o opts.o; rm -f * .o;
输出:
In file included from include/opts.c:5:0:
include/opts.h:14:16: error: array type has incomplete element type ‘struct opt’
struct opt avOpt[];
^~~~~
include/opts.h:28:17: error: flexible array member in a struct with no named members
struct opt *acOpt[];
^~~~~
为什么gcc不能编译我的文件?
在另一个项目中,我使用了这个代码并且它起了作用
现在它不起作用......
答案 0 :(得分:0)
看起来你正在声明一个结构,然后尝试给它另一个名字。尝试使用typedef然后只使用没有&#34; struct&#34;的新名称。这样的事情。
此外,您正在将内存大小化为指向结构的指针的大小,而不是结构的大小。
// == VAR ==
typedef struct _opt {
char *option; // e.g. --group
char *alias; // e.g. -G
int reqArg; // Require Argument | 0: No 1: Yes
int maxArgs; // -1: Undefined/ Unlimited
int func; /* Run Function? 0: No 1: Yes
* If No, it can be checked with function 'isOptEnabled'
*/
} opt_t;
typedef struct _optL {
opt_t avOpt[MAX_OPTS];
} optL_t;