gpt4 book ai didi

c - Getopt 不包括在内?函数 ‘getopt’ 的隐式声明

转载 作者:IT王子 更新时间:2023-10-29 00:23:21 26 4
gpt4 key购买 nike

我想使用 getopt,但它行不通。

它给了我

gcc -g -Wall -std=c99 -ftrapv -O2 -Werror -Wshadow -Wundef -save-temps -Werror-implicit-function-declaration   -c -o src/main.o src/main.c
src/main.c: In function ‘main’:
src/main.c:13:2: error: implicit declaration of function ‘getopt’ [-Werror=implicit-function-declaration]
src/main.c:23:14: error: ‘optarg’ undeclared (first use in this function)
src/main.c:23:14: note: each undeclared identifier is reported only once for each function it appears in
src/main.c:26:9: error: ‘optopt’ undeclared (first use in this function)
src/main.c:28:5: error: implicit declaration of function ‘isprint’ [-Werror=implicit-function-declaration]
src/main.c:36:5: error: implicit declaration of function ‘abort’ [-Werror=implicit-function-declaration]
src/main.c:36:5: error: incompatible implicit declaration of built-in function ‘abort’ [-Werror]
src/main.c:43:15: error: ‘optind’ undeclared (first use in this function)
cc1: all warnings being treated as errors
make: *** [src/main.o] Error 1

如果你想看,这里是源代码(几乎完全复制 getopt 联机帮助页)

#include <stdio.h>
#include <unistd.h> // getopt
#include "myfn.h"

int main(int argc, char *argv[])
{

int aflag = 0;
int bflag = 0;
char *cvalue = NULL;
int c;

while((c = getopt(argc, argv, "abc:")) != -1) {

switch(c) {
case 'a':
aflag = 1;
break;
case 'b':
bflag = 1;
break;
case 'c':
cvalue = optarg;
break;
case '?':
if (optopt == 'c')
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
else if (isprint(optopt))
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf (stderr, "Unknown option character `\\x%x'.\n", optopt);

return 1;

default:
abort ();
}

}

printf ("aflag = %d, bflag = %d, cvalue = %s\n", aflag, bflag, cvalue);

for (int i = optind; i < argc; i++) {
printf ("Non-option argument %s\n", argv[i]);
}

return 0;
}

知道我做错了什么吗?

我在 Linux 上,所以我认为它应该像这样工作。

最佳答案

尝试删除 -std=c99 .这可以防止在 <features.h> 中定义 POSIX 宏。 ,这可以防止 <unistd.h>来自包括 <getopt.h> .或者自己包含 getopt.h。

关于c - Getopt 不包括在内?函数 ‘getopt’ 的隐式声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22575940/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com