【问题标题】:Getopt not included? implicit declaration of function ‘getopt’不包括 Getopt?函数“getopt”的隐式声明
【发布时间】:2014-03-22 09:44:03
【问题描述】:

我想使用 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 手册页完全相同的 copypasta)

#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 上,所以我认为它应该像这样工作。

【问题讨论】:

  • 只是我用来调试makefile的东西,不重要。
  • 在这种情况下为什么要使用标志-implicit-function-declaration。
  • @tmp 即使只使用-g -Wall -std=c99,它也会给出完全相同的错误
  • 您在哪种系统上工作?你看过 unistd.h 吗?它包含 getopt 吗?
  • Xubuntu 12.04。当然可能是缺少一些包,但我不知道 getopt 应该在哪里。

标签: c linux getopt


【解决方案1】:

尝试删除-std=c99。这可以防止 POSIX 宏在&lt;features.h&gt; 中定义,从而防止&lt;unistd.h&gt; 包含&lt;getopt.h&gt;。 或者自己包含 getopt.h。

【讨论】:

  • 我会试试这个。但是我想在循环中声明循环变量,这就是我有 c99 的原因。
  • 没问题。只需将另一个 #include &lt;getopt.h&gt; 添加到您的来源即可。
  • @MightyPork 你可以试试-std=gnu99 而不是-std=c99.
  • 搞定了,std 标志确实是问题所在。现在愉快地使用-std=c99#include &lt;getopt.h&gt; 进行编译。但是我现在还需要unistd.h 吗? (顺便说一句,b/w c99 和 gnu99 有什么区别?)
  • getopt 是 C 的 GNU 扩展。如果您将自己限制为 ISO C (-std=c99),它将不可用。如果可以,请使用-std=gnu99,或手动包含getopt.h(请注意可移植性问题,因为并非所有平台都提供getopt.h
【解决方案2】:

你云不删除-std=c99。 而是在开头添加#define _POSIX_C_SOURCE 2

【讨论】:

    【解决方案3】:

    在包含中添加#include &lt;getopt.h&gt;

    【讨论】:

    • 这个建议已经是接受的答案的一部分
    【解决方案4】:

    绝对不需要更改-std 或直接包含getopt.h

    如果您想将 C99(或任何其他标准化)语言功能与 POSIX 函数(例如 getopt)一起使用,正确的做法是将 _POSIX_C_SOURCE 定义为正确的版本(例如,200809L)在包括相应的标题之前。有关详细信息,请参阅 feature_test_macros(7)。

    【讨论】:

      【解决方案5】:

      我遇到了同样的问题,解决方法是您很可能使用 -std=c99 进行编译,但请尝试使用 -std=gnu99,它应该可以工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-18
        • 1970-01-01
        • 2020-07-18
        • 2015-09-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多