Getopt未包含?隐式声明函数'getopt'。

20

我想使用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 functionmain’:
src/main.c:13:2: error: implicit declaration of functiongetopt[-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 functionisprint[-Werror=implicit-function-declaration]
src/main.c:36:5: error: implicit declaration of functionabort[-Werror=implicit-function-declaration]
src/main.c:36:5: error: incompatible implicit declaration of built-in functionabort[-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上,所以我认为它应该像这样工作。


@tmp 即使只使用“-g -Wall -std=c99”,它仍然会给出完全相同的错误。 - MightyPork
Xubuntu 12.04。当然可能会缺少一些软件包,但我无法弄清楚 getopt 应该在哪里。 - MightyPork
@MightyPork,这对我来说是有效的。你使用哪个Linux发行版?也可以先尝试简单的命令,如gcc prog.c -o prog,然后再使用makefile。 - Jayesh Bhoi
这是GCC 4.6.3版本。我尝试了gcc main.c -o main -std=c99,但它仍然显示之前的错误。 - MightyPork
我尝试过了,但效果不太好。现在问题已经得到解决,感谢@Guntram Blohm的帮助,也感谢大家的建议! - MightyPork
显示剩余5条评论
5个回答

47

尝试移除-std=c99。这会禁用GNU扩展,从而防止在<features.h>中定义POSIX宏,这会防止<unistd.h>包含<getopt.h>
或者将标志替换为-std=gnu99
或者自己包含getopt.h

getopt()unistd.h的一部分,这是GNU扩展。通过设置-std=c99,不再使用GNU扩展,该函数不再被声明,您需要显式地包含getopt.h


13
没问题,只需要在你的源代码中再添加一个 #include <getopt.h> 就可以了。 - Guntram Blohm
6
@MightyPork 你可以尝试使用“-std=gnu99”而不是“-std=c99”。 - Jayesh Bhoi
1
搞定了,std标志确实是问题所在。现在可以愉快地使用-std=c99#include <getopt.h>进行编译了。但是现在还需要unistd.h吗?顺便问一下,c99和gnu99有什么区别? - MightyPork
3
getopt 是 C 语言的 GNU 扩展。如果你限制自己使用 ISO C (-std=c99),它将不可用。如果可以的话,请使用 -std=gnu99,或手动包含 getopt.h(请注意可移植性问题,因为并非所有平台都提供 getopt.h)。 - sleblanc
1
getopt 不是 GNU 的扩展,而是 POSIX 标准化的。 - stefanct
显示剩余3条评论

3

您不能删除-std=c99。 相反,应在开头添加#define _POSIX_C_SOURCE 2


1
在包含部分添加#include <getopt.h>

2
这个建议已经包含在接受的答案中了。 - Tim Randall

0
没有必要更改 -std 或直接包含 getopt.h。如果您想使用 C99(或任何其他标准化)语言功能和 POSIX 函数(如 getopt),正确的做法是在包含相应头文件之前将 _POSIX_C_SOURCE 定义为正确的版本(例如 200809L)。有关详细信息,请参见 feature_test_macros(7)。

-4

我曾经遇到过同样的问题,解决方法是你很可能使用了-std=c99编译,而应该尝试使用-std=gnu99,这样就可以解决问题了。


这已经在接受的答案中解释过了。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接