gpt4 book ai didi

c - Solaris 中的 getopt 隐式声明?

转载 作者:太空狗 更新时间:2023-10-29 17:08:33 25 4
gpt4 key购买 nike

在 Solaris 中,gcc 给了我

implicit declaration of function `getopt'

编译时

#include <unistd.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
getopt(1,argv,"");
return 0;
}

man page for getopt 说了一些关于包含 unistd.h 或 stdio.h 的内容,但是即使我同时包含了这两者,我仍然会收到此警告。这是正常的吗?使用未明确声明的函数在 Unix 开发中是否常见?

最佳答案

您正在使用 -ansi 进行编译,并在该模式下 getopt可能不可用,因为 -ansi暗示 C89 一致性模式。尝试删除该开关,或 #define _GNU_SOURCE之前#include <unistd.h> . getopt()是 POSIX,不是 ANSI。

编辑:你可能不需要_GNU_SOURCE .根据this ,您应该能够通过定义预处理器宏来获得功能,从而实现这一点:

#if _POSIX_C_SOURCE >= 2 || _XOPEN_SOURCE || _POSIX_SOURCE

参见 this有关功能测试宏的更多信息。

关于c - Solaris 中的 getopt 隐式声明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1898057/

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